I am implementing clothing shopping cart and i am using session array for storing items to add to cart. now on place order button click i want that this cart table all rows which contains items on each row insert in to database. my code only inserts last row item and not inserting all rows data. here is my code:
<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 .");
}
}
?>