2016-01-19 47 views
0

我想要做的是从表单插入信用卡信息到我的MySQL数据库。 首先, 到期日期的输入格式将是MM/YY(有或没有空格)的表格上,我试图连接"20"字符串到年,但我总是得到"20 YY"作为结果,它不能从mysql被认定为日期。从PHP字符串到MySQL日期

我想获得过期日期以适应格式,以便稍后可以将其更改为MySQL格式。有任何想法吗?

这是我到目前为止的代码:

<?php 
       $cc_number=""; 
       $cc_expire=""; 
       $cc_cvc=""; 
       $cc_number=$_POST['number']; 
       $cc_expire=$_POST['expiry']; 
       $cc_cvc=$_POST['cvc']; 
       $cc_pid=$_SESSION['pid']; 

       /*edit the expire date*/ 
       $pieces=explode("/", $cc_expire); 
       $expire_dd="01"; 
       $expire_mm=$pieces[0]; 

       $expire_yy="20".$pieces[1]; 
       $expire_yy=trim($expire_yy, "\x00..\x1F"); 

       //$cc_expire = $expire_yy.$expire_mm.$expire_dd; 
       //$cc_expire = date('Y-m-d', strtotime(str_replace('-', '/', $cc_expire))); 
       echo "expire_yy = ".$expire_yy; 
       //echo "cc_expire = ".$cc_expire; 
       if (isset($_POST["submit"])) { 
        if (empty($cc_number) || empty($cc_cvc) || empty($cc_expire)) { 
         echo "<p align='center'><font color='red'><br><br><br><br>All fields are mandatory.</font></p>"; 
        } 
        else { 
         $cc_expire = date('Y-m-d', strtotime($cc_expire)); 
         $sql = "INSERT INTO credit_card (credit_card_number, cvc, expiration_date, pid) 
           VALUES ('$cc_number', '$cc_cvc', '$cc_expire', $cc_pid)"; 
         if ($dbconn->query($sql)) { 
          //echo "<script> window.location.assign('../card.php'); </script>"; 
         } 
         else { 
          echo "<p align='center'><font color='red'><br><br><br><br>Something went wrong.</font></p>"; 
         } 
        } 
       } 
      ?> 

我想对所有的牌来获得,这将是01-MM-YYYY MySQL的日期格式。

+0

'print_r($ _ POST)的输出是什么;'? – AnkiiG

+0

Mysql日期格式是yyyy-mm-dd,而不是'01-MM-YYYY'。什么是'expiration_date',varchar?示例代码中的$ cc_expire是什么?你也开放SQL注入这个.. – chris85

回答

0

根据数据看起来像在$ cc_expire您可能需要先分析它所以这第一:

$cc_expire = "28-".str_replace(" ","-",cc_expire); 

应该给DD-MM-yyyy格式(我已经使用'一问题/”改变日期格式时)

之前然后做:

$mysqldate = date("Y-m-d H:i:s", strtotime($cc_expire)); 

这应该转换cc_expire到MySQL格式准备插入。

+0

这是我最终需要的。但为了达到目的的第一步是创建日期变量。到目前为止'20 20 ** **与我不需要的空间**。 – Jack

+0

我已经更新了我的anwser –