2017-08-10 92 views
-1

我正在實施服裝購物網站,其中購物車物品插入到特定客戶的數據庫中。我有兩個表的順序和ordered_products。爲了表order_id是主鍵,這個表包含客戶信息。 Ordered_products表包含購物車中的產品。我試圖從訂單表中使用最後一個插入ID功能獲取訂單ID,但僅限於第一件物品需要訂單中的order_id以及所有其他物品需要來自ordered_products表的第一件物品。 這裏是我的PHP代碼,如何從另一張表中獲取最後一個插入標識在php

<?php 
    if (isset($_POST['placeorder'])) 
    { 
     $email=$_SESSION["email"]; 
     $con=mysqli_connect("localhost", "root", ""); 
     mysqli_select_db($con,"login"); 
     $qry="INSERT INTO orders (customer_id , customer_name, customer_email) VALUES ((SELECT user_id from users where email='$email'), (SELECT name from users where email='$email'), '$email')" ; 
     $result=mysqli_query($con,$qry) or die(mysqli_error($con)); 
     if($result) 
     {        
      for ($i=0; $i<count($_POST['ID']); $i++){ 
       $product_code = $_POST['ID'][$i]; 
       $gender = $_POST['gender'][$i]; 
       $price = $_POST['grandtotal'][$i];   
       $quantity = $_POST['qty'][$i];         
       $description = $_POST['description'][$i];   
       $qry="INSERT INTO ordered_product (order_id , product_code, product_description, Product_gender, Product_quantity, Product_price) VALUES ((SELECT LAST_INSERT_ID()), '$product_code', '$description', '$gender', '$quantity', '$price') "; 
       $result=mysqli_query($con,$qry) ; 
       if($result)   
       { 
        echo '<script>alert("Your order has been placed")</script>'; 
        echo '<script>window.location="portfolionew.php"</script>'; 
       } else { 
     die("Error While Adding Stock ! Please Try Again ."); 
     }      
    }}}   
    ?> 
+0

瞭解準備好的發言,以防止SQL注入 – Jens

回答

0

使用mysqli_insert_id

echo mysqli_insert_id($con) 

$qry="INSERT INTO orders (customer_id , customer_name, customer_email) VALUES ((SELECT user_id from users where email='$email'), (SELECT name from users where email='$email'), '$email')" ; 
$result=mysqli_query($con,$qry) or die(mysqli_error($con)); 
if($result) 
{ 
    echo mysqli_insert_id($con); 
    ... 
} 
0

可以更換插入查詢這一個。所以order_id對於所有人都是一樣的。

$order_id = mysqli_insert_id($con); 
$qry="INSERT INTO ordered_product (order_id , product_code, product_description, Product_gender, Product_quantity, Product_price) VALUES ('$order_id', '$product_code', '$description', '$gender', '$quantity', '$price') "; 
$result=mysqli_query($con,$qry) ; 
+0

其工作不適合插入相同的ID,但這樣做同樣 –

0

這裏的問題是

執行這個查詢後

$ QRY =「INSERT INTO訂單(CUSTOMER_ID,CUSTOMER_NAME,CUSTOMER_EMAIL)VALUES((從用戶選擇USER_ID,其中電子郵件= $電子郵件」 ),(電子郵件=「$ email」的用戶的SELECT名稱),'$ email')「;

SELECT LAST_INSERT_ID()將你的執行之後被ORDER_ID

$ QRY =「INSERT INTO ordered_product(ORDER_ID,PRODUCT_CODE,PRODUCT_DESCRIPTION,Product_gender,Product_quantity,PRODUCT_PRICE)VALUES((SELECT LAST_INSERT_ID() ),'$ product_code','$ description','$ gender','$ quantity','$ price')「;

然後LAST_INSERT_ID()將使用ordered_product表主鍵值進行更新。由於這個你得到的問題。

嘗試將臨時變量中的LAST_INSERT_ID()保存並使用。

與以下替換您的代碼,

<?php 
    if (isset($_POST['placeorder'])) 
    { 
     $email=$_SESSION["email"]; 
     $con=mysqli_connect("localhost", "root", ""); 
     mysqli_select_db($con,"login"); 
     $qry="INSERT INTO orders (customer_id , customer_name, customer_email) VALUES ((SELECT user_id from users where email='$email'), (SELECT name from users where email='$email'), '$email')" ; 
     $result=mysqli_query($con,$qry) or die(mysqli_error($con)); 
     $order_id = mysql_insert_id(); 
     if($result) 
     {        
      for ($i=0; $i<count($_POST['ID']); $i++){ 
       $product_code = $_POST['ID'][$i]; 
       $gender = $_POST['gender'][$i]; 
       $price = $_POST['grandtotal'][$i];   
       $quantity = $_POST['qty'][$i];         
       $description = $_POST['description'][$i];   
       $qry="INSERT INTO ordered_product (order_id , product_code, product_description, Product_gender, Product_quantity, Product_price) VALUES ($order_id, '$product_code', '$description', '$gender', '$quantity', '$price') "; 
       $result=mysqli_query($con,$qry) ; 
       if($result)   
       { 
        echo '<script>alert("Your order has been placed")</script>'; 
        echo '<script>window.location="portfolionew.php"</script>'; 
       } else { 
     die("Error While Adding Stock ! Please Try Again ."); 
     }      
    }}}   
    ?> 
+0

由於@kranthi它的工作完美:) –

+0

這是偉大的...... ...................... – kranthi

相關問題