我正在为学校创建一个杂货店,我创建的每个订单都应该从数据库中获取一个唯一的orderID。我应该能够让用户使用orderID查看唯一的订单,因此它总是相同的事实是一个问题。 to orderID设置为自动递增,但每次提交订单时,它都使用相同的orderID。这是我的代码
$email = $_SESSION['email'];
$name = $_SESSION['firstname'];
$shipping = $_SESSION['shipping'];
$ordertotal = $_SESSION['ordertotal'];
$tax = $_SESSION['tax'];
$productid = $_SESSION['productid'];
$count = $_SESSION['itemquantity'];
$linetotal = $_SESSION['linetotal'];
//get customer id
$custID = mysqli_query($connection, "SELECT CustomerID FROM Customers WHERE
Email ='$email' AND FirstName ='$name' ");
$customerid = mysqli_fetch_assoc($custID);
foreach($customerid as $i)
//print_r($i);
//insert customer id
$insertcustID = mysqli_query($connection, "INSERT INTO Orders (CustomerID)
VALUES ('$i') ");
//get order id
$orderid = mysqli_query($connection, "SELECT OrderID FROM Orders
WHERE CustomerID = '$i'
AND OrderDate IS NULL");
$id = mysqli_fetch_assoc($orderid);
foreach($id as $r)
print_r($r);
//get date
date_default_timezone_set("UTC");
$mydate=getdate(date("U"));
foreach($mydate as $d)
//echo "$mydate[weekday], $mydate[month] $mydate[mday], $mydate[year]";
//insert the order
$setOrder = mysqli_query($connection, "UPDATE Orders SET
ShippingCost='$shipping', Tax='$tax', Total='$ordertotal', OrderDate=' $d ’
WHERE OrderID='$r'
AND CustomerID='$i' ");
$insertOrder = mysqli_query($connection, "INSERT INTO OrderDetails
(OrderID, ProductID, Quantity, LineTotal)
VALUES ('$r', '$productid', '$count', '$linetotal')");
$allorders = mysqli_query($connection, "SELECT * FROM OrderDetails");
foreach($allorders as $q)
print_r($q);
unset($_SESSION["cart"]); //resets cart after order is submitted
include("realfooter.html");
答案 0 :(得分:0)
哦......似乎$ i是一个局部变量,你在foreach
中声明了$ i变量foreach($customerid as $i)
//print_r($i);
//insert customer id
$insertcustID = mysqli_query($connection, "INSERT INTO Orders (CustomerID)
VALUES ('$i') ");
//get order id
$orderid = mysqli_query($connection, "SELECT OrderID FROM Orders
WHERE CustomerID = '$i'
你试着把一个临时变量放在foreach之外,它会存储变量的值
$customer_id ="";
foreach($customerid as $i) {
//print_r($i);
$customer_id = $i;
//insert customer id
$insertcustID = mysqli_query($connection, "INSERT INTO Orders (CustomerID)
VALUES ('$i') ");
}
//get order id
$orderid = mysqli_query($connection, "SELECT OrderID FROM Orders
WHERE CustomerID = '$customer_id'
AND OrderDate IS NULL");
或者为$ custID
设置LIMIT//get customer id
$custID = mysqli_query($connection, "SELECT CustomerID FROM Customers WHERE
Email ='$email' AND FirstName ='$name' LIMIT =1 ");
$customerid = mysqli_fetch_assoc($custID);
//insert customer id
$insertcustID = mysqli_query($connection, "INSERT INTO Orders (CustomerID)
VALUES ('".$customerid['CustomerID']."') ");
//get order id
$orderid = mysqli_query($connection, "SELECT OrderID FROM Orders
WHERE CustomerID = '".$customerid['CustomerID']."'
AND OrderDate IS NULL");
$id = mysqli_fetch_assoc($orderid);
foreach($id as $r)
print_r($r);