【问题标题】:Sql query for insert multiple array values into database用于将多个数组值插入数据库的 Sql 查询
【发布时间】:2018-09-23 19:40:25
【问题描述】:

我从购物车表中获取数组值,我想将购物车数组值插入到我的订单表中,但我面临的问题只是最后一个数组值只插入到我的数据库中,但我想将所有购物车值插入到我的订单表。

/*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 */

【问题讨论】:

标签: php sql arrays


【解决方案1】:

由于您的表单元素是数组 [],因此像这样循环它们:

$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];

    // ...

}

【讨论】:

猜你喜欢
  • 2014-03-03
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多