Php 只有最后一行数据插入到数据库中,而不是所有行数据
我正在实现服装购物车,并使用会话数组存储要添加到购物车中的项目。现在,单击PlaceOrder按钮,我想将这张购物车表中包含每行项目的所有行插入数据库。我的代码只插入最后一行项目,而不是插入所有行数据。 这是我的密码:Php 只有最后一行数据插入到数据库中,而不是所有行数据,php,html,Php,Html,我正在实现服装购物车,并使用会话数组存储要添加到购物车中的项目。现在,单击PlaceOrder按钮,我想将这张购物车表中包含每行项目的所有行插入数据库。我的代码只插入最后一行项目,而不是插入所有行数据。 这是我的密码: <table align="center" > <thead> <tr > <th>Product</th> <th>Price</th>
<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 .");
}
}
?>
产品
价格
数量
小计
产品代码:
PKR正如其他人提到的,开始使用mysqli
现在对于您的特定问题,它只插入一行,因为您的隐藏字段设置为仅绑定到一行
在这个例子中,我只使用一个输入。您需要为每个输入更改此设置。而不是
<input type="hidden" name="ID" value="<?php echo $values["item_id"]; ?>" >
<input type="hidden" name="description" value=...... etc etc
每次在新代码中使用数据库扩展时,它都会被弃用,并且已经存在多年,在PHP7中永远消失。如果你只是在学习PHP,那么就把精力花在学习PDO
或mysqli
数据库扩展和准备好的语句上。您好,通过在提交检查中打印来转储您的帖子数据,这样我们可以对出现的问题进行排序。像这个回声“;打印(邮政美元);你真的应该升级到MySQLi或PDO<代码>mysql*
函数在PHP 5中被弃用,在PHP 7中被删除您的脚本甚至有被使用的风险
<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.