Jump to content

Help needed with debugging code


hass1980

Recommended Posts

Argggh, i need help with debugging these errors im having, its driving me nuts as I cant seem to correct them.

 

I keep getting the following error messages on the web page.

 

Notice: Undefined variable: itemsres in C:\wamp\www\website\functions.php on line 71

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\website\functions.php on line 71

 

Notice: Undefined variable: total in C:\wamp\www\website\functions.php on line 109

 

Notice: Undefined index: SESS_USERID in C:\wamp\www\website\functions.php on line 30

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\website\functions.php on line 33

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\website\functions.php on line 38

 


<?php
function pf_validate_number($value, $function, $redirect) {
if(isset($value) == TRUE) {
if(is_numeric($value) == FALSE) {
$error = 1;
}
if($error == 1) {
header("Location: " . $redirect);
}
else {
$final = $value;
}
}
else {
if($function == 'redirect') {
header("Location: " . $redirect);
}
if($function == "value") {
$final = 0;
}
}
return $final;
}
function showcart()
{
if($_SESSION['SESS_ORDERNUM'])
{
$custsql = "SELECT id, status from
orders WHERE customer_id = "
. $_SESSION['SESS_USERID']
. " AND status < 2;";
$custres = mysql_query($custsql);
$custrow = mysql_fetch_assoc($custres);
$itemssql = "SELECT products.*, orderitems.*, orderitems.id AS
itemid FROM products, orderitems WHERE orderitems.product_id =
products.id AND order_id = " . $custrow['id'];
$itemsres = mysql_query($itemssql);
$itemnumrows = mysql_num_rows($itemsres);
}
else
{
$custsql = "SELECT id, status from orders WHERE session = '" . session_id() . "' AND status < 2;";
$custres = mysql_query($custsql);
$custrow = mysql_fetch_assoc($custres);
$itemssql = "SELECT products.*,
orderitems.*, orderitems.id AS itemid
FROM products, orderitems WHERE
orderitems.product_id = products.id AND
order_id = " . $custrow['id'];
$itemsres = mysql_query($itemssql);
$itemnumrows = mysql_num_rows($itemsres);
// If no SESS_ORDERNUM variable is available, the $itemnumrows variable is set to 0:
$itemnumrows = mysql_num_rows($itemsres);
}
}
if(isset($itemnumrows) && $itemnumrows == 0)
{
echo "You have not added anything to your shopping cart yet.";
}
else
{
echo "<table cellpadding='10'>";
echo "<tr>";
echo "<td></td>";
echo "<td><strong>Item</strong></td>";
echo "<td><strong>Quantity</strong></td>";
echo "<td><strong>Unit Price</strong></td>";
echo "<td><strong>Total Price</strong></td>";
echo "<td></td>";
echo "</tr>";
while($itemsrow = mysql_fetch_assoc($itemsres))
{
$quantitytotal =
$itemsrow['price'] * $itemsrow['quantity'];
echo "<tr>";
if(empty($itemsrow['image'])) {
echo "<td><img
src='./productimages/dummy.jpg' width='50' alt='"
. $itemsrow['name'] . "'></td>";
}
else {
echo "<td><img src='./productimages/" .
$itemsrow['image'] . "' width='50' alt='"
. $itemsrow['name'] . "'></td>";
}
echo "<td>" . $itemsrow['name'] . "</td>";
echo "<td>" . $itemsrow['quantity'] . "</td>";
echo "<td><strong>£"
. sprintf('%.2f', $itemsrow['price'])
. "</strong></td>";
echo "<td><strong>£"
. sprintf('%.2f', $quantitytotal) . "</strong></td>";
echo "<td>[<a href='"
. $config_basedir . "delete.php?id="
. $itemsrow['itemid'] . "'>X</a>]</td>";
echo "</tr>";
$total = $total + $quantitytotal;
$totalsql = "UPDATE orders SET total = "
. $total . " WHERE id = "
. $_SESSION['SESS_ORDERNUM'];
$totalres = mysql_query($totalsql);
}
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td>TOTAL</td>";
echo "<td><strong>£"
. sprintf('%.2f', $total) . "</strong></td>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
echo "<p><a href='checkout-address.php'>Go to the checkout</a></p>";
}
?>



 

 

 

Link to comment
https://forums.phpfreaks.com/topic/154211-help-needed-with-debugging-code/
Share on other sites

The undefined variable/index messages are because your code is accessing variables that don't exist at the time the code runs. If those variables should exist, you need to find out why they are not present. If those variables are optional, they might or might not exist when the code runs, you need to use the isset() or empty() functions to test if they exist.

 

All of the mysql_fetch_xxxx errors are because you queries are failing and your code is not checking if the query worked before blindly attempting to access non-existent data. Queries can fail if there is no connection to the database server, there is no database selected, or there is an syntax error in the query that prevented it from executing.

 

For debugging why your queries are failing, you need to add an or die(mysql_error()) on the end of each mysql_query() statement, for example -

 

$custres = mysql_query($custsql) or die("Query failed: " . mysql_error());

 

 

All I've done is indented it properly to make it easier to read and moved the closing bracket at line 55 to the end of the code. It all looks about right but I've not tested obviously.

 

<?php
function pf_validate_number($value, $function, $redirect) {
if(isset($value) == TRUE) {
	if(is_numeric($value) == FALSE) {
		$error = 1;
	}
	if($error == 1) {
		header("Location: " . $redirect);
	}
	else {
		$final = $value;
	}
}
else {
	if($function == 'redirect') {
		header("Location: " . $redirect);
	}
	if($function == "value") {
		$final = 0;
	}
}
return $final;
}

function showcart()
{
if($_SESSION['SESS_ORDERNUM'])
{
	$custsql = "SELECT id, status from
	orders WHERE customer_id = "
	. $_SESSION['SESS_USERID']
	. " AND status < 2;";
	$custres = mysql_query($custsql);
	$custrow = mysql_fetch_assoc($custres);
	$itemssql = "SELECT products.*, orderitems.*, orderitems.id AS
	itemid FROM products, orderitems WHERE orderitems.product_id =
	products.id AND order_id = " . $custrow['id'];
	$itemsres = mysql_query($itemssql);
	$itemnumrows = mysql_num_rows($itemsres);
}
else
{
	$custsql = "SELECT id, status from orders WHERE session = '" . session_id() . "' AND status < 2;";
	$custres = mysql_query($custsql);
	$custrow = mysql_fetch_assoc($custres);
	$itemssql = "SELECT products.*,
	orderitems.*, orderitems.id AS itemid
	FROM products, orderitems WHERE
	orderitems.product_id = products.id AND
	order_id = " . $custrow['id'];
	$itemsres = mysql_query($itemssql);
	$itemnumrows = mysql_num_rows($itemsres);
	// If no SESS_ORDERNUM variable is available, the $itemnumrows variable is set to 0:
	$itemnumrows = mysql_num_rows($itemsres);
}
if(isset($itemnumrows) && $itemnumrows == 0)
{
	echo "You have not added anything to your shopping cart yet.";
}
else
{
	echo "<table cellpadding='10'>";
	echo "<tr>";
	echo "<td></td>";
	echo "<td><strong>Item</strong></td>";
	echo "<td><strong>Quantity</strong></td>";
	echo "<td><strong>Unit Price</strong></td>";
	echo "<td><strong>Total Price</strong></td>";
	echo "<td></td>";
	echo "</tr>";
	while($itemsrow = mysql_fetch_assoc($itemsres))
	{
		$quantitytotal =
		$itemsrow['price'] * $itemsrow['quantity'];
		echo "<tr>";
		if(empty($itemsrow['image'])) {
			echo "<td><img
			src='./productimages/dummy.jpg' width='50' alt='"
			. $itemsrow['name'] . "'></td>";
		}
		else {
			echo "<td><img src='./productimages/" .
			$itemsrow['image'] . "' width='50' alt='"
			. $itemsrow['name'] . "'></td>";
		}
		echo "<td>" . $itemsrow['name'] . "</td>";
		echo "<td>" . $itemsrow['quantity'] . "</td>";
		echo "<td><strong>£"
		. sprintf('%.2f', $itemsrow['price'])
		. "</strong></td>";
		echo "<td><strong>£"
		. sprintf('%.2f', $quantitytotal) . "</strong></td>";
		echo "<td>[<a href='"
		. $config_basedir . "delete.php?id="
		. $itemsrow['itemid'] . "'>X</a>]</td>";
		echo "</tr>";
		$total = $total + $quantitytotal;
		$totalsql = "UPDATE orders SET total = "
		. $total . " WHERE id = "
		. $_SESSION['SESS_ORDERNUM'];
		$totalres = mysql_query($totalsql);
	}
	echo "<tr>";
	echo "<td></td>";
	echo "<td></td>";
	echo "<td></td>";
	echo "<td>TOTAL</td>";
	echo "<td><strong>£"
	. sprintf('%.2f', $total) . "</strong></td>";
	echo "<td></td>";
	echo "</tr>";
	echo "</table>";
	echo "<p><a href='checkout-address.php'>Go to the checkout</a></p>";
}
}

 

 

 

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.