RynMan Posted June 12, 2010 Share Posted June 12, 2010 Hi guys I'm basically trying to implement this cart into my pages: http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart I understand it for the mostpart (I'm a novice at PHP but can generally figure out what's going on here). When I run this code on my cart display page I get the following error: <?php function showCart() { $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart_add.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM Items WHERE ItemID = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="cart_add.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$ItemName.' by '.$author.'</td>'; $output[] = '<td>£'.$UnitPrice.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>£'.($UnitPrice * $qty).'</td>'; $total += $UnitPrice * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: £'.$total.'</p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); } showCart(); ?> Notice: Undefined variable: db in C:\wamp\www\Adept\cart_add.php on line 88 Fatal error: Call to a member function query() on a non-object in C:\wamp\www\Adept\cart_add.php on line 88 Line 88 is pointing to this line of code $result = $db->query($sql); I realise that $db isn't defined....but should it be? I think I'm missing something here. I looked through all of the poster's code and nowhere does he define that variable....maybe the function isn't returning anything? Thanks for any help guys. Quote Link to comment Share on other sites More sharing options...
ignace Posted June 12, 2010 Share Posted June 12, 2010 I realise that $db isn't defined....but should it be? Notice: Undefined variable: db in C:\wamp\www\Adept\cart_add.php on line 88 What do you think? Where do you define $db - if anywhere? Quote Link to comment Share on other sites More sharing options...
RynMan Posted June 12, 2010 Author Share Posted June 12, 2010 I realise that $db isn't defined....but should it be? Notice: Undefined variable: db in C:\wamp\www\Adept\cart_add.php on line 88 What do you think? Where do you define $db - if anywhere? Well....that's my question. Is it defined somewhere that I'm missing? I can't see it defined anywhere in my code, nor the code the poster on that page has written, however that means very little (as I said I was a PHP novice). Quote Link to comment Share on other sites More sharing options...
RynMan Posted June 12, 2010 Author Share Posted June 12, 2010 That site seems to be down right now. The same tutorial is posted here: http://www.studyblog.net/2008/11/a-simple-php-shopping-cart-script/ Quote Link to comment Share on other sites More sharing options...
ignace Posted June 12, 2010 Share Posted June 12, 2010 (*Note*: I’m using a PHP class to handle my database connections, so your code may need to be slightly different). It seems like you will need to adjust the code to make it work with the database. Quote Link to comment Share on other sites More sharing options...
RynMan Posted June 12, 2010 Author Share Posted June 12, 2010 If someone else had any ideas id be extremely grateful. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.