Path problem for add to cart button

Hi there, i follow a tutorial from PHP MySQL Shopping Cart Tutorial and i’m having path problem for ‘add to cart’ button.

Demo from phpwebcommerce: My Online Shop
I have modified some changes, i’m putting the ‘add to cart’ button to 1st level instead of 2nd level. Example, under all category (car & manga) i choose car, then inside car category list there is a ‘add to cart’ button under all car product instead of clicking inside and show ‘add to cart’ button.

productDetail.php:


<?php
if (!defined('WEB_ROOT')) {
	exit;
}

$product = getProductDetail($pdId, $catId);

// we have $pd_name, $pd_price, $pd_description, $pd_image, $cart_url
extract($product);
?> 
<table width="100%" border="0" cellspacing="0" cellpadding="10">
 <tr> 
  <td align="center"><img src="<?php echo $pd_image; ?>" border="0" alt="<?php echo $pd_name; ?>" width="230" height="170"></td>
  <td valign="middle">
<strong><?php echo $pd_name; ?></strong><br>
Price : <?php echo displayAmount($pd_price); ?><br>
<?php
// if we still have this product in stock
// show the 'Add to cart' button
if ($pd_qty > 0) {
?>
<input type="button" name="btnAddToCart" value="Add To Cart >" onClick="window.location.href='<?php echo $cart_url; ?>';" class="addToCartButton">
<?php
} else {
	echo 'Out Of Stock';
}
?>
  </td>
 </tr>
 <tr align="left"> 
  <td colspan="2"><?php echo $pd_description; ?></td>
 </tr>
</table>


I copy this code out from productDetail.php:


<input type="button" name="btnAddToCart" value="Add To Cart >" onClick="window.location.href='<?php echo $cart_url; ?>';">

productList.php:


<?php
if (!defined('WEB_ROOT')) {
	exit;
}

$productsPerRow = 2;
$productsPerPage = 8;

//$productList    = getProductList($catId);
$children = array_merge(array($catId), getChildCategories(NULL, $catId));
$children = ' (' . implode(', ', $children) . ')';

$sql = "SELECT pd_id, pd_name, pd_price, pd_thumbnail, pd_qty, c.cat_id
		FROM tbl_product pd, tbl_category c
		WHERE pd.cat_id = c.cat_id AND pd.cat_id IN $children 
		ORDER BY pd_name";
$result     = dbQuery(getPagingQuery($sql, $productsPerPage));
$pagingLink = getPagingLink($sql, $productsPerPage, "c=$catId");
$numProduct = dbNumRows($result);

// the product images are arranged in a table. to make sure
// each image gets equal space set the cell width here
$columnWidth = (int)(100 / $productsPerRow);
?>
<table width="100%" border="0" cellspacing="0" cellpadding="20">

<?php 
if ($numProduct > 0 ) {

	$i = 0;
	while ($row = dbFetchAssoc($result)) {
	
		extract($row);
		if ($pd_thumbnail) {
			$pd_thumbnail = WEB_ROOT . 'images/product/' . $pd_thumbnail;
		} else {
			$pd_thumbnail = WEB_ROOT . 'images/no-image-small.png';
		}
	
		if ($i % $productsPerRow == 0) {
			echo '<tr>';
		}

		// format how we display the price
		$pd_price = displayAmount($pd_price);
		
		echo "<td width=\\"$columnWidth%\\" align=\\"center\\"><a href=\\"" . $_SERVER['PHP_SELF'] . "?c=$catId&p=$pd_id" . "\\"><img src=\\"$pd_thumbnail\\" border=\\"0\\" width=\\"230\\" height=\\"170\\"><br>$pd_name</a><br>Price : $pd_price<br>
		<input type=\\"button\\" name=\\"btnAddToCart\\" value=\\"Add To Cart >\\" onClick=\\"window.location.href='<?php echo $cart_url; ?>';\\" >";
		
        

		// if the product is no longer in stock, tell the customer
		if ($pd_qty <= 0) {
			echo "<br>Out Of Stock";
		}
		
		echo "</td>\\r\
";
	
		if ($i % $productsPerRow == $productsPerRow - 1) {
			echo '</tr>';
		}
		
		$i += 1;
	}
	
	if ($i % $productsPerRow > 0) {
		echo '<td colspan="' . ($productsPerRow - ($i % $productsPerRow)) . '">&nbsp;</td>';
	}
	
} else {
?>
	<tr><td width="100%" align="center" valign="center">No products in this category</td></tr>
<?php	
}	
?>
</table>
<p align="center"><?php echo $pagingLink; ?></p>

The code i copy from productDetail.php i paste in productList.php at line48, it show the ‘add to cart’ button but when i click on it, it cant add to my shopping cart and it show error:

Notice: Undefined variable: cart_url in C:\Domains\xxxxxxxx\wwwroot\xxxxxxxx\plaincart\include\productList.php on line 48
Notice: Undefined variable: cart_url in C:\Domains\xxxxxxxx\wwwroot\xxxxxxxx\plaincart\include\productList.php on line 48
Notice: Undefined variable: cart_url in C:\Domains\xxxxxxxx\wwwroot\xxxxxxxx\plaincart\include\productList.php on line 48
Notice: Undefined variable: cart_url in C:\Domains\xxxxxxxx\wwwroot\xxxxxxxx\plaincart\include\productList.php on line 48

Will Appreciate For Any Help…Thank you in advanced & sorry for the bad English.

Thank you…i’ll try to improve my debugging skills

I can’t run or step through your code for you because I don’t have all your code or database. A significant part of coding involves developing debugging skills otherwise you’ll be in forums like this forever looking for someone to debug code for you.

To debug your code, you can insert echo statements at various places to follow the flow of your code and the values of variables as the code is running. When an echo that should execute doesn’t or a variable has an incorrect value, that area of your code will be the source of a problem.
Since you’re new to php, pehaps take on a smaller project first and work through the w3schools php tutorials.

Hi webdev1958, thank you for helping & for the fast reply :slight_smile:
I’m still new in php, not too sure how to define it :frowning:

i added this code:


$cart_url = "cart.php?action=add&p=$pdId";

The code i added is from product-functions.php:


<?php
require_once 'config.php';

/*********************************************************
*                 PRODUCT FUNCTIONS 
**********************************************************/


/*
	Get detail information of a product
*/
function getProductDetail($pdId, $catId)
{
	
	$_SESSION['shoppingReturnUrl'] = $_SERVER['REQUEST_URI'];
	
	// get the product information from database
	$sql = "SELECT pd_name, pd_description, pd_price, pd_image, pd_qty
			FROM tbl_product
			WHERE pd_id = $pdId";
	
	$result = dbQuery($sql);
	$row    = dbFetchAssoc($result);
	extract($row);
	
	$row['pd_description'] = nl2br($row['pd_description']);
	
	if ($row['pd_image']) {
		$row['pd_image'] = WEB_ROOT . 'images/product/' . $row['pd_image'];
	} else {
		$row['pd_image'] = WEB_ROOT . 'images/no-image-large.png';
	}
	
	$row['cart_url'] = "cart.php?action=add&p=$pdId";
	
	return $row;			
}

?>

I’m still getting error, for the ‘add to cart’ button, after i click on it, it should not link to any page, it should just add the item into shopping cart.

This is the shopping cart script, miniCart.php:


<?php
if (!defined('WEB_ROOT')) {
	exit;
}

$cartContent = getCartContent();

$numItem = count($cartContent);	
?>
<table width="103%" border="1" cellspacing="0" cellpadding="2" id="minicart">
 <?php
if ($numItem > 0) {
?>
 <tr>
  <td colspan="2">Cart Content</td>
 </tr>
<?php
	$subTotal = 0;
	for ($i = 0; $i < $numItem; $i++) {
		extract($cartContent[$i]);
		$pd_name = "$ct_qty x $pd_name";
		$url = "index.php?c=$cat_id&p=$pd_id";
		
		$subTotal += $pd_price * $ct_qty;
?>
 <tr>
   <td width="64%"><a href="<?php echo $url; ?>"><?php echo $pd_name; ?></a></td>
   
  <td width="36%" align="right"><?php echo displayAmount($ct_qty * $pd_price); ?></td>
 </tr>
<?php
	} // end while
?>
  <tr><td align="right">Sub-total</td>
  <td width="36%" align="right"><?php echo displayAmount($subTotal); ?></td>
 </tr>
  <tr><td align="right">Shipping</td>
  <td width="36%" align="right"><?php echo displayAmount($shopConfig['shippingCost']); ?></td>
 </tr>
  <tr><td align="right">Total</td>
  <td width="36%" align="right"><?php echo displayAmount($subTotal + $shopConfig['shippingCost']); ?></td>
 </tr>
  <tr><td colspan="2">&nbsp;</td></tr>
  <tr>
  <td colspan="2" align="center"><a href="cart.php?action=view"> Go To Shopping 
   Cart</a></td>
 </tr>  
<?php	
} else {
?>
  <tr><td colspan="2" align="center" valign="middle">Shopping Cart Is Empty</td></tr>
<?php
}
?> 
</table>


:sick::sick:

In productDetail.php you have this line:

onClick="window.location.href='<?php [B][COLOR=Red]echo $cart_url; [/COLOR][/B]?>';"

but you haven’t defined or got $cart_url anywhere previously in productDetail.php. Before you can echo, or do anything, with a variable yuo have to define it or get it from somewhere first.

To debug your code, you can insert echo statements at various places to follow the flow of your code and the values of variables as the code is running. When an echo that should execute doesn’t or a variable has an incorrect value, that area of your code will be the source of a problem.