Jump to content

sessions


Ruth

Recommended Posts

I'm attempting a simple shopping cart to try and learn sessions, so far it's been horrible. I have several links that will display a selection of books. If the user clicks on one book it will show the description of that book. They then can click add to chart at this point I want to store the book information aka order into a session, but have been unsuccessful. I find these simple examples of code online that do not help. Can anyone help me figure this out?

 

Here is what is in my session page

session_start ();

$_SESSION ['cart'] = array();

$_SESSION ['image'] = $image;

$_SESSION ['bktitle'] = $bktitle;

$_SESSION ['price'] = $price;

$_SESSION ['qty'] = $qty;

$_SESSION ['totalqty'] = $totalqty;

$_SESSION ['totalprice'] = $totalprice;

$_SESSION ['total'] = $total;

 

show cart page

session_start ();

 

if (isset ($_SESSION['cart']))

{

$_SESSION['cart'] ++;

}

else

{

$_SESSION['cart'] = 1;

}

 

$isbn = $_GET['isbn'];

 

if (!$_GET['cart'])

{

if (!$isbn)

{

echo 'You have no books in your cart. Please add a book.';

}

}

require_once ('dbconnection.php');

 

if ($isbn or $_GET['cart'])

{

$query = "SELECT image_path, title, book_price

from books

where isbn = '$isbn'";

$result = mysqli_query ($db, $query) or die (mysqli_error ($db));

 

while ($row = mysqli_fetch_array ($result))

{

$image = $row ['image_path'];

$bktitle = $row ['title'];

$price = $row ['book_price'];

$count = count ($row['title']);

}

$qty = 1;

$totalqty = $qty + $_POST ['qty'];

$totalprice = $price * $qty;

$total = $totalprice * $count ;

require_once ('session.php');

 

echo<<<stop

<form method = "post" action = "show_cart.php">

<table>

<tr bgcolor = "red">

<th colspan = "2" align = "center"><font color = "white">Item</font></th>

<th><font color = "white">Price</font></th>

<th><font color = "white">Qty</font></th>

<th><font color = "white">Total</font></th>

</tr>

<tr>

<tfoot bgcolor = "red">

<td></td>

<td></td>

<td></td>

<td><font color = "white">$totalqty </font></td>

<td><font color = "white">$total</font></td>

 

</tfoot>

</tr>

<tr>

<td><img src = "$image"></td>

<td valign = "top">$bktitle</td>

<td valign = "top">$price</td>

<td valign = "top"><input type = "text" value = "$qty" name = "qty" size ="2"></td>

<td valign = "top">$totalprice</td>

</tr>

</table>

</form>

stop;

}

 

 

Link to comment
https://forums.phpfreaks.com/topic/96202-sessions/
Share on other sites

I really don't know if I'm doing this right. I turned on error_reporting(E_ALL); and I get Undefined index: cart, Undefined index: qty, and A session had already been started - ignoring session_start() . However I also did print_r($_SESSION); and I get Array ( [cart] => Array ( ) [image] => /images/java_book.gif [bktitle] => Java 2 for professional Developers [price] => 34.99 [qty] => 1 [totalqty] => 1 [totalprice] => 34.99 [total] => 34.99 ). I'm not real sure if the session is being set at all.

Link to comment
https://forums.phpfreaks.com/topic/96202-sessions/#findComment-492470
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.