我正在實施服裝購物網站,其中購物車物品插入到特定客戶的數據庫中。我有兩個表的順序和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 .");
}
}}}
?>
瞭解準備好的發言,以防止SQL注入 – Jens