Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过php向php表插入日期失败_Php_Mysql - Fatal编程技术网

通过php向php表插入日期失败

通过php向php表插入日期失败,php,mysql,Php,Mysql,我显示了一个购物车,用户必须从日历小部件中选择交货日期,然后单击确认按钮。提交时,订单购物车应与输入的交货日期一起填充 我使用的代码是: <?php echo "<form action='' name='form1' >"; //some disaply codes here echo "Choose the Date of delivery<input type='date' name='date'>"; echo

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

我使用的代码是:

<?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);
   }
}

?>     
订单表没有填充。我不能指出这个错误。请指出


编辑:orders表已被禁用,但del date字段为空…请让我知道如何正确传递日期变量,因为很明显这就是问题所在

我不确定您想说的是什么。你的代码块缺少很多东西。例如:

$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
编辑

让我试着用propper缩进为您重做一下

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);
             }
         }    
     }
 }

订单表中的dt_del是日期类型echo out$queury_insert并复制粘贴到phpmyadminc可以尝试diemsql_错误吗?这将向您显示错误有一个通知,我收到通知:未定义索引:日期这意味着您的日期设置不正确。你的约会是怎么来的?大多数情况下,这意味着变量是空的,而它需要一个变量,因此您需要检查日期变量的来源。我看不到它在您的代码中定义。当用户确认订单时,保存在购物车表中的所有项目都会复制到订单表中。是的,我错过了执行步骤..我正在执行代码:$query=select*fromcart$结果=mysqli_query$conn$query;接下来是while语句据我所知,这段代码的问题在于日期变量……我是php新手,这是基于我到目前为止所学的内容……请指导我如何更好、正确地处理它,但我正在定义代码……:echo选择交付日期;在insert php代码中,我使用$date=stripslashes$_POST['date'];不,因为你在while循环之外告诉变量,然后它就会忘记它。因此,在本例中,移动$date=stripslashes$_POST['date'];在你的while循环中。在while循环中,您只能访问结果中返回的内容,以及您定义的所有其他内容。所以,把你的$date移到里面,它应该会起作用。