通过php失败的日期插入到php表

时间:2014-10-20 09:31:54

标签: php mysql

我显示了购物车,用户必须从日历小部件中选择交货日期,然后单击确认按钮。在提交时,订单购物车应与输入的交货日期一起填充。

我使用的代码是:

<?php
echo "<form action='' name='form1' >";

//some disaply codes here

    echo "Choose the Date of delivery<input type='date' name='date'>";


                echo "<input type='submit' class='btn btn-default' name='final_submission' value ='Confirm Order'>";
                echo "</form>";      
  ?>

插入的代码是:

<?php
if (isset($_POST['final_submission'])){
                    error_reporting(E_ALL);
                    ini_set('display_errors', 1);

                        $IP="my localhost";
                        $USER="my user name";
                        $conn=mysqli_connect($IP,$USER,"","my database name");
                        if (mysqli_connect_errno()) {
                            echo "Failed to connect to MySQL: " . mysqli_connect_error();
                        }


$date = stripslashes($_POST['date']);
$result=mysqli_query($conn, $query);
$query="select * from cart";

while($row = mysqli_fetch_array($result)) {
    $us_id=$row['user_id'];
    $pr_id=$row['prod_id'];
    $qtty=$row['quantity'];

    $query_insert = "insert into orders(user_id, prod_id, quantity, dt_del) values('.$us_id.',
    '.$pr_id.', '.$qtty.','.$date.')";
    $res_ins=mysqli_query($conn,$query_insert);
   }
}

?>     

订单表未填充。我无法指出错误。请点出来

编辑:订单表格正在填充,但是del日期字段显示为空白...请让我知道如何正确传递日期变量,这显然是问题

1 个答案:

答案 0 :(得分:0)

我不确定你试图说实话。你的代码块缺少很多东西。例如:

$query="select * from cart";
while($row = mysqli_fetch_array($result)) {

结果如何?你在哪里定义它们?这是你在那里指定的查询的结果,因为你肯定不会执行它。因此结果将为0并且不会被执行。

问题还在于这个查询它将遍历购物车的每一行,这意味着你也会抓住属于你的顾客的购物车,指定。

所以你的代码应该是

$query="select * from cart";
$result = mysqli_query($conn,$query);
while($row=mysqli_fetch_array($result)){
// yadi yadi yadi .. code code code

修改

让我尝试用擅自缩进的方法为你重做这个。

if (isset($_POST['final_submission'])){   // check if the button is correct
    // error_reporting(E_ALL);        <-- canceling this out for now
    // ini_set('display_errors', 1);  <-- canceling this out for now

       $IP="my localhost";
       $USER="my user name";
       $conn=mysqli_connect($IP,$USER,"","my database name");
       if (mysqli_connect_errno()) {
           echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }

     $date = stripslashes($_POST['date']);

     if (empty($date) || $date == ''){
          echo 'the date is not set, please check if parameter is filled in';   
     } else { // if it wont pass this point, you wont get an enormous output of unidentified indexes

         $query="select * from cart";
         $result=mysqli_query($conn, $query);
         while($row = mysqli_fetch_array($result)) {
             $us_id=$row['user_id'];
             $pr_id=$row['prod_id'];
             $qtty=$row['quantity'];
             $date = stripslashes($_POST['date']);  // just to play it safe define it again

             if (empty($date) || $date == ''){
                echo 'something went wrong with the date';
             }else{ 
                 $query_insert = "insert into orders(user_id, prod_id, quantity, dt_del) values('.$us_id.','.$pr_id.', '.$qtty.','.$date.')";
                 $res_ins=mysqli_query($conn,$query_insert);
             }
         }    
     }
 }