从db中检索数据并将其作为输入提供给另一个查询?

时间:2018-03-29 17:29:43

标签: php

<?php  
                if(isset($_POST["place_order"]))  
                {  
                    $customer_id= "SELECT max(CustomerID) FROM tbl_customer"; //this will select newly entered customerID
                 $result5 = mysqli_query($connect,$customer_id);  //here query will run and store the result in $result5
                 $result2 = mysqli_fetch_array($result5);


                     $insert_order = "  
                     INSERT INTO tbl_order(customer_id, creation_date, order_status)  
                     VALUES('$result2["CustomerID"]', '".date('Y-m-d')."', 'pending')"; 
                    /*here is problem..I want to give  "$result 5" as input in the query where there is VALUES('$result 2["Customer ID"]',____) for inserting into customer_id but not it is not inserting it..
                      "$customer_id" query runs properly I have tried it in php-my-admin */
    ?>     

我试图在phpmyadmin中运行我的查询[$ customer_id]它运行正常..

但是当我试图回显“CustomerId”时,它没有被显示,而且我的数据也没有插入到数据库中。 谢谢。

3 个答案:

答案 0 :(得分:0)

您需要删除查询中的引号。

试试这个:

<?php

 if(isset($_POST["place_order"]))  
 {  
     $customer_id= "SELECT max(CustomerID) FROM tbl_customer";
     $result5 = mysqli_query($connect,$customer_id);
     $result2 = mysqli_fetch_array($result5);    

     if($result2 == true) { // checking if there is a result
         $customer_id = $result2['CustomerID'];
     } else {
        $customer_id = 0;
     }      

     $insert_order = "INSERT INTO tbl_order(customer_id, creation_date, order_status) VALUES('".$result2['CustomerID']."', '".date('Y-m-d')."', 'pending')"; 
 }

?>     

答案 1 :(得分:0)

你可以试试这个:

STContains()

答案 2 :(得分:0)

我认为CustomerID是数字,所以不得引用它。

 $insert_order = "INSERT INTO tbl_order(customer_id, creation_date, order_status) VALUES(".$result2[0].", '".date('Y-m-d')."', 'pending')";