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

If someone can order more books, why is there no loop in your code (for every cart item)?

The books are added from my catalogue page which stores the information in a session and then sends them to the view cart page which is above.

This is the code for my catalogue page:

<?php 
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Catalogue</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" >
<?php echo "Welcome ".$_SESSION['login_user'];?>! <br> <a href="logout.php">Logout</a>
</div>
</div>

<br><br>
    <h1 id = "mainHeader" >Books</h1>
<br>   
   <div class="books">
        <?php
            //current URL of the Page. cart_update.php redirects back to this URL
            $current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

$results = $mysqli->query("SELECT * FROM books ORDER BY Category ASC");
if ($results) { 

    //fetch results set as object and output HTML
    while($obj = $results->fetch_object())
    {
        echo '<div class="book">'; 
        echo '<form method="post" id = "books" action="cart_update.php">';
        echo '<div class="book-thumb"><img src="images/'.$obj->BookImage.'"></div>';
        echo '<div class="book-content"><h3>'.$obj->Title.'</h3>';
        echo '<br>';
        echo '<div class="book-author"><i>'.$obj->BookAuthor.'</i></div>';
        echo '<div class="book-desc">'.$obj->BookDesc.'</div>';
        echo '<br>';
        echo '<div class="book-qty"> In stock: '.$obj->Quantity.'</div>';
        echo '<div class="book-info">';
        echo 'Price '.$currency.$obj->Price.' | ';
        echo 'quantity <input type="text" name="Quantity" value="1" size="3" />';
        echo '<button class="add_to_cart">Add To Cart</button>';
        echo '</div></div>';
        echo '<input type="hidden" name="ISBN" value="'.$obj->ISBN.'" />';
        echo '<input type="hidden" name="type" value="add" />';
        echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
        echo '</form>';
        echo '</div>';
    }
}
?>
</div>


<div class="shopping-cart">
<h2>Your Shopping Cart</h2>
<?php
if(isset($_SESSION["books"]))
{
$total = 0;
    echo '<ol>';
    foreach ($_SESSION["books"] as $cart_itm)
    {
        echo '<li class="cart-itm">';
    echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["ISBN"].'&return_url='.$current_url.'">&times;</a></span>';
    echo '<h3>'.$cart_itm["Title"].'</h3>';
    echo '<div class="p-ISBN">ISBN : '.$cart_itm["ISBN"].'</div>';
    echo '<div class="p-quantity">Quantity : '.$cart_itm["quantity"].'</div>';
    echo '<div class="p-Price">Price :'.$currency.$cart_itm["Price"].'</div>';
    echo '</li>';
    $subtotal = ($cart_itm["Price"]*$cart_itm["quantity"]);
    $total = ($total + $subtotal);
}
echo '</ol>';
echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php">Go to checkout</a></span>';
echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'">Empty Cart</a></span>';
}else{
echo 'Your Cart is empty';
}
?>
</div>

</div>

</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.