我目前正在开发一个数据库,我希望将order_ID放入两个表中。这有效,但我遇到的问题是循环只迭代一次。如果有人可以提供帮助并解释我出错的地方,我们将不胜感激。
session_start();
echo $_SESSION['shop_id'];
$shopid = $_SESSION['shop_id'];
$username = $_SESSION['username'];
$con = mysqli_connect('localhost', 'root', '', 'aurora');
if (!isset($con)) {
echo "Connection to Aurora System failed.";
}
if (isset($_POST['items'])) {
echo "True";
} else {
echo "false";
}
$valid = true;
$date = date('l jS \of F Y h:i:s A');
$sql2 = "INSERT INTO orders_new (user_submitted, order_date, customer_ID) VALUES ('$username', '$date', '$shopid')";
if ($valid == true) {
$ordersubmit2 = mysqli_query($con, $sql2);
echo "Success!";
}
$count = $_POST['items'];
for ($i = 1; $i <= $count; $i++) {
$idinsert = mysqli_insert_id($con);
$product = $_POST['product'.$i];
$nicotine = $_POST['nicotine'.$i];
$qty = $_POST['qty'.$i];
echo $product;
$sql = "INSERT INTO orders_detail (orders_id ,product, variant, quantity) VALUES ('$idinsert', '".$product."', '".$nicotine."', '".$qty."')";
$ordersubmit = mysqli_query($con, $sql);
}
答案 0 :(得分:1)
是我自己的错,order_id是主键,因此循环因仅允许唯一值而停止。