Ruth Posted March 7, 2008 Share Posted March 7, 2008 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 More sharing options...
Ruth Posted March 7, 2008 Author Share Posted March 7, 2008 The problem is in the query I can run the query in the cmd but it only works for a few of the records. The records it does not work in cmd, will display empty set? Does anyone know what I'm doing wrong? Link to comment https://forums.phpfreaks.com/topic/94909-shopping-cart/#findComment-486160 Share on other sites More sharing options...
Northern Flame Posted March 7, 2008 Share Posted March 7, 2008 if ($isbn or $_GET['cart']) well if you are checking for either $isbn or $_GET['cart'] it does not mean that $isbn will be set all the time. Link to comment https://forums.phpfreaks.com/topic/94909-shopping-cart/#findComment-486207 Share on other sites More sharing options...
Ruth Posted March 7, 2008 Author Share Posted March 7, 2008 Yes isbn is set but does it really matter, the query is not working for every selection thats why $row is being set. Link to comment https://forums.phpfreaks.com/topic/94909-shopping-cart/#findComment-486245 Share on other sites More sharing options...
Northern Flame Posted March 7, 2008 Share Posted March 7, 2008 well the query also has "WHERE" in there, maybe some rows dont fit the requirement.... Link to comment https://forums.phpfreaks.com/topic/94909-shopping-cart/#findComment-486260 Share on other sites More sharing options...
Ruth Posted March 7, 2008 Author Share Posted March 7, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.