0

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 class="save_order">Save Order</button>';
    echo '</form>';

}else{
    echo 'Your Cart is empty';
}

?>
</div>
</div>
</body>
</html>

on line 75 i have a button called "save_order"

How would i go about writing an INSERT MySQL statement based on when the button is clicked
Id want it to insert the ISBN number, the price of each book as well as the user name of the person that is stored in the session which is called login_user.

Anything will help!

Thanks!

2
Contributors
11
Replies
50
Views
2 Years
Discussion Span
Last Post by spud91
0

Before form you want to check out if data is sent by POST so let's say right after line 40 (or first line after opening if condition) you need something like:

if ( isset($_POST['submit_btn']) ) {
    // do what you need
    // insert or what ever
    // don't forget validation check of input data
} else {
    // here is code you have now actually
}

For this working, you need to give name to your button.
In example above, I named it:
<button name="sumbit_btn" class="save_order">Save Order</button>
Don't forget that this code need to be before form. You can set it on start of the page er where ever you find fitting.

0

Hi,

I cant seem to get it to work.

I forgot to mention that the fields that are stored in the "books" session such as ISBN, title etc need to be saved as well

Any ideas as to how i would to that?

Thanks!

0

One issue at time, please.
Or at least open new topic for such an issue too.

I cant seem to get it to work.

What error you got?

0

Sorry about that.
I dont get an error.
im confused as to how to add the session variables from the above code into the database

Edited by spud91

0

I am not sure what is your issue: getting session dedicating value to it or something third. I tried to answer from topic title.
Next, I can't see anywhere on page you checks if user actually IS logged.
Seems like you would get error of not isset $_SESSION['login_user'].
After session_start() function which need to stay at the top of page (mostly of time, and main reason is to be sure there is NO OUTPUT before instantiating sessions by session_start() function), you are encouraged to set values to specific session variable. You need to simply understand that sessions are array. If you go with print_r($_SESSION);exit; you'll get array output. You can set nested array but be aware to not overwrite particular values:

session_start();
$_SESSION['color']['girl'] = 'red';
$_SESSION['color']['boy'] = 'blue';
$_SESSION['color'] = 'green';//will overwrite two lines above

So my question is where is instantiated $_SESSION['login_user'] before? :)
If not instantiated, what is error/message you are getting there in line 29?

To continue text, if you need to use it that way, like setting sessions array, you would maybe consider of serializing sessions to store it like string. Again, you would pull out it from DB and deserialize it to array before use.

My appology if saying more than needed, but I am not 100 pct clear with your issue or it is not 100 pct clarified.

Edited by Tpojka: code backticks

0

Hi I actually have a login page where $_SESSION['login_user'] is instantiated
this is the first thing the user has to do before they can access the books.

Below is the code that the user will see before they access the "view_cart" page which has been posted in my first post:

<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>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" >
<?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.'">×</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>`

in this code the user has to click view cart which will send all the information such as the ISBN numer and the title of the book to the next page which is view cart. The code for view cart is in my original post:

My issue is on on the view cart page.

I have a button on the view cart page and i want to be able to save the user id, isbn number, title, quantity and the total price from the view cart page into the database when the user has clicked on save order.

Im stuck on the insert statement and how i would access the session variables in order to add them to the database.

Hope that makes more sense. Sorry i didnt show you the code in my original post

Edited by spud91

0

I would use AJAX to update cart.
Is there DB table already? Can you describe it?
I really don't know where did you stuck?
Do you know to insert / update data to table?

0

Hi i cant use Ajax. Has to be mysql
Yes i have a table called "orders" it has 6 columns in it:
OrderNo, BookName, Quantity, Total, ISBN, StudentID

I know how to do normal insert statement but not sure how to insert value from session.

0

Simpliest way would be:
UPDATE orders SET ISBN='$_SESSION["ISBN"]' WHERE OrderNo='$_SESSION["OrderNo"]';
if that is the question.
Still can't see where is the problem though.
Keep asking, maybe someone else come to conversation. :)

0

Thats an update statement
I need insert statement, I am using WAMP server phpmyadmin for my tables.

The OrderNo is auto increment so i wouldnt use that in the insert statement.

Still really confused as to how i can insert the variables into my database.

0

Hi,

My insert statement doesnt work:

<?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"]))
{
    if(isset($_POST['submit_btn']) ){
        $sql = "INSERT INTO `orders` (`OrderNo`,`BookName`,`Quantity`,`TotalPrice`,`ISBN`,`StudentID`) VALUES (NULL, '{$obj->Title}', '{$cart_itm['quantity']}', '{$total}', '{$ISBN}', '{$_SESSION['login_user']}');";     
        }else {

        $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="sumbit_btn" class="save_order">Save Order</button>';
        echo '</form>';

    }
}


?>
</div>
</div>
</body>
</html>

I dont get any errors when pressing the button.

And nothing gets inserted into my database

Any ideas why?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.