我正在实施服装购物车,我正在使用会话数组来存储要添加到购物车的商品。现在在地点订单按钮单击我希望此购物车表中包含每行上的项目的所有行都插入到数据库中。我的代码只插入最后一行而不插入所有行数据。 这是我的代码:
<table align="center" >
<thead>
<tr >
<th>Product</th>
<th>Price</th>
<th>QTY</th>
<th>SubTotal</th>
</tr>
</thead>
<?php
if (!empty($_SESSION["shopping_cart"])) {
$total=0;
foreach ($_SESSION["shopping_cart"] as $keys => $values) {
?>
<tr>
<td><p>Product Code:<?php echo $values["item_id"]; ?> <br/>
<?php echo $values["item_description"]; ?> </p></td>
<td>PKR<input type="number" name="price1" id="price1" value="<?php echo $values["item_price"];?>" readonly ></td>
<td><input type="number" name="qty[<?php echo $values["item_id"]; ?>]" id="qty" value="<?php echo $values["item_quantity"];?>" readonly></td>
<td>PKR<input type="number" name="total" id="total" value="<?php echo ($values["item_quantity"] * $values["item_price"]) ?>" readonly></td>
</tr>
<?php
$total=$total+($values['item_quantity']*$values["item_price"]);
}
}
?>
</table>
<input type="hidden" name="ID" value="<?php echo $values["item_id"]; ?>" >
<input type="hidden" name="gender" value="<?php echo $values["item_gender"]; ?>" >
<input type="hidden" name="description" value="<?php echo $values["item_description"]; ?>" >
<input type="hidden" name="qty" value="<?php echo $values["item_quantity"];?>" >
<input type="hidden" name="grandtotal" value="<?php echo $total ?>" >
<button style="margin-left: 750px;" type="submit" name="submit" class="btn btn-primary btn-lg">Place Order</button>
<?php
if (isset($_POST['submit'])) {
$product_code = $_POST['ID'];
$gender = $_POST['gender'];
$price = $_POST['grandtotal'];
$quantity = $_POST['qty'];
$description = $_POST['description'];
$email=$_SESSION["email"];
$con=mysql_connect("localhost", "root", "");
mysql_select_db("login",$con);
$qry="INSERT INTO order1 ( order_description , product_code, gender, order_quantity, order_price, customer_name, email, customer_id) VALUES ('$description', '$product_code', '$gender', '$quantity', '$price', (SELECT name from users where email='$email'), '$email', (SELECT user_id from users where email='$email') ) ";
}
$result=mysql_query($qry,$con);
if($result) {
echo '<script>alert("Your order has been placed")</script>';
echo '<script>window.location="portfolionew.php"</script>';
} else {
die("Error While Adding Stock ! Please Try Again .");
}
}
?>
答案 0 :(得分:0)
正如其他人所说,开始使用mysqli
现在针对您的具体问题,它只插入一行,因为您的隐藏字段设置为仅绑定到一行。
在这个例子中,我只使用一个输入。您需要为每个输入更改此值。而不是
<input type="hidden" name="ID" value="<?php echo $values["item_id"]; ?>" >
<input type="hidden" name="description" value=...... etc etc
它应该设置为数组
<input type="hidden" name="ID[]" value="<?php echo $values["item_id"]; ?>" >
<input type="hidden" name="description[]" value=...... etc etc
对于插入,您需要像这样设置
$id[]=$_POST['ID'];
$description[]=$_POST['description'];
foreach($id as $key => $value){
$d=$description[$key]; /////// the id is key that binds every field in that row
insert into table (value1, value2) values ($value, $d. etc etc)
} /////// NOTE FOREACH IS after the insert query.