Jump to content

Recommended Posts

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.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/204558-help-with-my-cart/
Share on other sites

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). 

 

 

Link to comment
https://forums.phpfreaks.com/topic/204558-help-with-my-cart/#findComment-1071072
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.