2016-01-19 49 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 –