Jump to content

shopping cart


Ruth

Recommended Posts

I'm trying to build a shopping cart but I've run into problems. I'm passing the isbn from page to page through a link and that variable gets set. The problem is when I move down to while ($row = mysqli_fetch_array ($result)) the variable $row does not always get set. $query and $result always gets set but not $row. Some records will be set and others will not. I cannot get every selection to display. Here is my code.

 

<?php

//shopping cart session

session_start ();

$_SESSION ['cart'] = NULL;

require_once ('session.php');

 

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

{

$_SESSION['cart'] = $_GET['cart'];

}

else

{

$_SESSION['cart'] = 0;

}

 

$title = 'view cart';

require_once ('header.inc');

 

echo '<h1><font color = "#C01209"><center>Your Shopping Cart</center></font></h1>';

 

$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 b.image_path, b.title, b.book_price, o.qty

from books b, order_items o

where b.isbn = o.isbn and b.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'];

$qty  = $row ['qty'];

}

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

$totalprice = $price * $qty;

 

//display results

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">$totalprice</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">$total</td>

</tr>

</table>

</form>

stop;

}

//?cat=1 and 2

echo '<p align = "center"><a href = "show_cart.php?cart=1"><img src = "/images/save-changes.gif" border = "0px"></a></p>';

echo '<p><a href = "index.php"><img src = "/images/continue-shopping.gif" border = "0px"></a></p>';

echo '<p><a href = "checkout.php?cart=1"><img src = "/images/go-to-checkout.gif" border = "0px"></a></p>';

 

mysqli_close ($db);

require_once ('footer.inc');

?>

Link to comment
https://forums.phpfreaks.com/topic/94909-shopping-cart/
Share on other sites

I'm not sure what you mean. I was only checking one result not them all, so I assumed everyone would work. I started working on sessions and thats when I noticed the query wasn't working correctly. I commented out the stuff I add for the sessioins which is the !$_GET['cart'] and it sill didn't work. I don't understand why some of the isbn's will work in the query and others will not.

Link to comment
https://forums.phpfreaks.com/topic/94909-shopping-cart/#findComment-486292
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.