我正在实施服装购物网站。我在订购时遇到问题只有购物车中的最后一行项目HTML表格插入数据库而不是所有行。我希望当我点击下订单按钮时,所有行值都应插入数据库中。因此,客户应将所有选定商品放入购物车中以便订购。 这是代码,
<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_gender"]; ?><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"];?>"></td>
<input type="hidden" name="qty" value="<?php echo $values["item_quantity"];?>" >
<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"]);
/* $total=$total+$values["item_price"]; */
}
} ?>
</table> <br/><br/>
<button type="submit" name="submit" class="btn btn-primary btn-lg">Place Order</button>
here is php code,
if (isset($_POST['submit']))
{
$product_code = $_POST['ID'];
$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 order ( order_description , product_code, order_quantity, order_price, customer_name, email, customer_id) VALUES ('$description', '$product_code', '$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 :(得分:-1)
<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;
$i = 1;
foreach ($_SESSION["shopping_cart"] as $keys => $values) {
?>
<tr>
<td style=" text-align: left;" >
<p style="margin-top: -120px; margin-left: 150px;">Product Code:<?php echo $values["item_id"]; ?> <br/>
<?php echo $values["item_gender"]; ?><br/>
<?php echo $values["item_description"]; ?> </p></td>
<td style="text-align: left; width: 80px;">PKR<br/>
<input style="width: 70px;" type="number" name="price<?= $i; ?>" id="price<?= $i; ?>" value="<?php echo $values["item_price"];?>" readonly ></td>
<td><input type="number" name="qty_<?= $i; ?>" id="qty_<?= $i; ?>" value="<?php echo $values["item_quantity"];?>"></td>
<td style="text-align: left; width: 80px;"> <b>PKR</b>
<br/> <input style="width: 70px; " 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"]);
$i++;
/* $total=$total+$values["item_price"]; */
}
} ?>
<input hidden="iTotalProducts" value="<?php echo $i; ?>" name="product_count">
</table> <br/><br/>
<button type="submit" name="submit" class="btn btn-primary btn-lg">Place Order</button>
//here is php code,
if (isset($_POST['submit']))
{
$product_code = $_POST['ID'];
$price = $_POST['grandtotal'];
$quantity = $_POST['qty'];
$description = $_POST['description'];
$email=$_SESSION["email"];
$con=mysql_connect("localhost", "root", "");
mysql_select_db("login",$con);
$cnt = $_POST["iTotalProducts"];
for($i = 1; $i <= $cnt; $i++) {
// here you can write your insert code
}
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 .");
}
} `enter code here`