简体   繁体   中英

PHP - For loop only writing one submission to MySql

I'm currently working on a database where I would like the order_ID's to be put into two tables. This works but the issue I have is that the loop is only iterating once. If anyone could help and explain where I have gone wrong it would be greatly appreciated.

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);
}

是我自己的错,order_id是主键,因此由于只允许唯一值,所以循环停止了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM