Hi Guys!
i have the following code:
<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View shopping cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css"></head>
<body>
<br>
<div id="books-wrapper">
<!-- #content to center the menu -->
<div id="content">
<!-- This is the actual menu -->
<ul id="darkmenu">
<li><a href="home.php">Home</a></li>
<li><a href="catalogue.php">Catalogue</a></li>
<li><a href="search.php">Search</a></li>
<li><a href= "view_cart.php">Cart</a></li>
<li><a href="#">Orders</a></li>
</ul>
<div id = "welcome" >
Welcome, <?=$_SESSION['login_user']?>! <br> <a href="logout.php">Logout</a>
</div>
</div>
<br><br>
<h1 id = "mainHeader" >View Cart</h1>
<br>
<div class="view-cart">
<?php
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION["books"]))
{
$total = 0;
echo '<form method="post" action="">';
echo '<ul>';
$cart_items = 0;
foreach ($_SESSION["books"] as $cart_itm){
$ISBN = $cart_itm["ISBN"];
$results = $mysqli->query("SELECT Title,BookDesc,Price FROM books WHERE ISBN='$ISBN'");
$obj = $results->fetch_object();
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["ISBN"].'&return_url='.$current_url.'">×</a></span>';
echo '<div class="p-Price">'.$currency.$obj->Price.'</div>';
echo '<div class="book-info">';
echo '<h3>'.$obj->Title.' (ISBN :'.$ISBN.')</h3> ';
echo '<div class="p-quantity">Quantity : '.$cart_itm["quantity"].'</div>';
echo '<div>'.$obj->BookDesc.'</div>';
echo '</div>';
echo '</li>';
$subtotal = ($cart_itm["Price"]*$cart_itm["quantity"]);
$total = ($total + $subtotal);
echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->Title.'" />';
echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$ISBN.'" />';
echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->BookDesc.'" />';
echo '<input type="hidden" name="item_quantity['.$cart_items.']" value="'.$cart_itm["quantity"].'" />';
$cart_items ++;
}
echo '</ul>';
echo '<span class="check-out-txt">';
echo '<strong>Total : '.$currency.$total.'</strong> ';
echo '</span>';
echo '<button name="submit_btn" class="save_order">Save Order</button>';
echo '</form>';
if(isset($_POST['submit_btn']) ){
$insquery = "INSERT INTO `pending_orders` (`OrderNo`,`BookName`,`Quantity`,`TotalPrice`,`ISBN`,`StudentID`) VALUES (NULL, '" . $obj->Title . "', '" . $cart_itm['quantity'] . "', '" . $total . "', '" . $ISBN . "', '" . $_SESSION['login_user'] . "');";
$stmt = $mysqli->prepare($insquery);
$stmt->execute();
}
}
?>
</div>
</div>
</body>
</html>
The code is supposed to save a customers book order into a database, It works perfectly fine for ONE book.
however if the person has purchased two books then only the last book gets added to the database. Screenshots added:
This is what it looks like on my website. As you can see the person has selected two books to be purchased:
http://postimg.org/image/3kj1gvytx/
However this is what i get in my phpmyadmin site:
http://postimg.org/image/k85hs56xj/
We can see that only the second book (biology) gets stored in the database. as well as the total of the two books
Any ideas how i could fix the issue and store both books.
Thanks