简体   繁体   中英

Sql query for insert multiple array values into database

I'm getting array value from cart table and I want to insert cart array value into my order table but the problem I was facing is only last array value only inserting into my database but I want to insert all cart value into my order table.

 /*This is the form page I was getting cart value*/ <form> <input type="text" name="cid[]" value="1"> <input type="text" name="pid[]" value="2"> <input type="text" name="quantity[]" value="3"> <input type="text" name="total[]" value="5"> </form> /*Once the form is submited the action comes to this php page*/ <?php $cid = $_POST['cid']; $pid = $_POST['pid']; $quantity = $_POST['quantity']; $total = $_POST['cid']; $insertquery = "INSERT INTO orders(cid,pid,quantity,total) VALUES('$cid','$pid','$quantity','$total')"; ?> /*After excution of this code only inseting the last value, not inserting the all value */ 

Since your form elements are arrays [], loop over them like this:

$rowCount = count($_POST['cid']);

for($=0; $i < $rowCount; $i++) {

    $cid = $_POST['cid'][$i];
    $pid = $_POST['pid'][$i];
    $quantity = $_POST['quantity'][$i];
    $total = $_POST['total'][$i];

    // ...

}

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