Jump to content

[SOLVED] Undefined Variable/Supplied Argument Is Not Valid


twilitegxa

Recommended Posts

I am getting the following errors when running my php page:

 

Notice: Undefined variable: get_item_res in C:\wamp\www\showitem.php on line 16

 

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

 

Here is my code for the page:

 

<?php
//connect to database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

$display_block = "<h1>My Store - Item Detail</h1>";

//validate item
$get_item = "select c.cat_title, si.item_title, si.item_price,
si.item_desc, si.item_image from store_items as si left join
store_categories as c on c.id = si.cat_id where si.id = $_GET[item_id]";

$get_items_res = mysql_query($get_item) or die(mysql_error());

if (mysql_num_rows($get_item_res) < 1) {
//invalid item
$display_block .= "<p><em>Invalid item selection.</em></p>";
} else {
//valid item, get info
$cat_title = strtoupper(stripslashes(
	mysql_result($get_item_res,0,'cat_title')));
$item_title = stripslashes(mysql_result($get_item_res,0, 'item_title'));
$item_price = mysql_result($get_item_res,0, 'item_price');
$item_desc = stripslashes(mysql_result_($get_item_res,0, 'item_desc'));
$item_image = mysql_result($get_item_res,0, 'item_image');

//make breadcrumb trail
$display_block .= "<p><strong>You are viewing:</em><br>
<a href=\"seestore.php?cat_id\">$cat_title</a>
	> $item_title</strong></p>

	<table cellpadding=3 cellspacing=3>
	<tr>
	<td valign=middle align=center><img src=\"$item_image\"></td>
	<td valign=middle><p><strong>Description:</strong><br>$item_desc</p>
	<p><strong>Price:</strong> \$$item_price</p>";

	//get colors
	$get_colors = "select item_color from store_item_color where
		item_id = $item_id order by item_color";
	$get_colors_res = mysql_query($get_colors) or die(mysql_error());

	if (mysql_num_res($get_colors_res) > 0) {

		$display_block .= "<p><strong>Available Colors:</strong><br>";

		while ($colors = mysql_fetch_array($get_colors_res)) {
			$item_color = $colors['item_color'];

			$display_block .= "$item_color<br>";
		}
	}

	//get sizes
	$get_sizes = "select item_size from store_item_size where
		item_id = $item_id order by item_size";
	$get_sizes_res = mysql_query($get_sizes) or die(mysql_error());

	if (mysql_num_rows($get_sizes_res) > 0) {

		$display_block .= "<p><strong>Available Sizes:</strong><br>";

		while ($sizes = mysql_fetch_array($get_sizes_res)) {
			$item_size = $sizes['item_size'];

			$display_block .= "$item_size<br>";
		}
	}

	$display_block .= "
	</td>
	</tr>
	</table>";

}
?>
<html>
<head>
<title>My Store</title>
</head>
<body>
<?php print $display_block; ?>
</body>
</html>

 

What am I doing wrong here?

That fixed that problem, but now I am receiving this error:

 

Notice: Undefined variable: item_id in C:\wamp\www\showitem.php on line 41

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order by item_color' at line 2

 

Here is the revised code:

 

<?php
//connect to database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

$display_block = "<h1>My Store - Item Detail</h1>";

//validate item
$get_item = "select c.cat_title, si.item_title, si.item_price,
si.item_desc, si.item_image from store_items as si left join
store_categories as c on c.id = si.cat_id where si.id = $_GET[item_id]";

$get_item_res = mysql_query($get_item) or die(mysql_error());

if (mysql_num_rows($get_item_res) < 1) {
//invalid item
$display_block .= "<p><em>Invalid item selection.</em></p>";
} else {
//valid item, get info
$cat_title = strtoupper(stripslashes(
	mysql_result($get_item_res,0,'cat_title')));
$item_title = stripslashes(mysql_result($get_item_res,0, 'item_title'));
$item_price = mysql_result($get_item_res,0, 'item_price');
$item_desc = stripslashes(mysql_result($get_item_res,0, 'item_desc'));
$item_image = mysql_result($get_item_res,0, 'item_image');

//make breadcrumb trail
$display_block .= "<p><strong>You are viewing:</em><br>
<a href=\"seestore.php?cat_id\">$cat_title</a>
	> $item_title</strong></p>

	<table cellpadding=3 cellspacing=3>
	<tr>
	<td valign=middle align=center><img src=\"$item_image\"></td>
	<td valign=middle><p><strong>Description:</strong><br>$item_desc</p>
	<p><strong>Price:</strong> \$$item_price</p>";

	//get colors
	$get_colors = "select item_color from store_item_color where
		item_id = $item_id order by item_color";
	$get_colors_res = mysql_query($get_colors) or die(mysql_error());

	if (mysql_num_res($get_colors_res) > 0) {

		$display_block .= "<p><strong>Available Colors:</strong><br>";

		while ($colors = mysql_fetch_array($get_colors_res)) {
			$item_color = $colors['item_color'];

			$display_block .= "$item_color<br>";
		}
	}

	//get sizes
	$get_sizes = "select item_size from store_item_size where
		item_id = $item_id order by item_size";
	$get_sizes_res = mysql_query($get_sizes) or die(mysql_error());

	if (mysql_num_rows($get_sizes_res) > 0) {

		$display_block .= "<p><strong>Available Sizes:</strong><br>";

		while ($sizes = mysql_fetch_array($get_sizes_res)) {
			$item_size = $sizes['item_size'];

			$display_block .= "$item_size<br>";
		}
	}

	$display_block .= "
	</td>
	</tr>
	</table>";

}
?>
<html>
<head>
<title>My Store</title>
</head>
<body>
<?php print $display_block; ?>
</body>
</html>

 

What did I do wrong on this one? I must be missing something, but I can't see it! Can anyone help?

xtopolis,

 

store_categories as c on c.id = si.cat_id where si.id = $_GET[item_id]";

 

This may grab something from the categories. But your idea may be correct, this is the ONLY tag listed in that script that can grab and identify $item_id.

The previous page is where the viewer selects the item to view the details on:

 

seestore.php

<?php
//connect to database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

$display_block = "<h1>My Categories</h1>
<p>Select a category to see its items.</p>";

//show categories first
$get_cats = "select id, cat_title, cat_desc from
store_categories order by cat_title";
$get_cats_res = mysql_query($get_cats) or die(mysql_error());

if (mysql_num_rows($get_cats_res) < 1) {
$display_block = "<p><em>Sorry, no categories to browse.</em></p>";
} else {

while ($cats = mysql_fetch_array($get_cats_res)) {
	$cat_id = $cats['id'];
	$cat_title = strtoupper(stripslashes($cats['cat_title']));
	$cat_desc = stripslashes($cats['cat_desc']);

	$display_block .= "<p><strong><a 
	href=\"$_SERVER[php_SELF]?cat_id=$cat_id\">$cat_title</a></strong>
	<br>$cat_desc</p>";

	if(isset($_GET['cat_id']) && $_GET['cat_id'] == $cat_id) {
		//get items
		$get_items = "select id, item_title, item_price
		from store_items where cat_id = $cat_id
		order by item_title";
		$get_items_res = mysql_query($get_items) or die(mysql_error());

		if (mysql_num_rows($get_items_res) < 1) {
			$display_block = "<p><em>Sorry, no items in
				this category.</em></p>";
			} else {

				$display_block .= "<ul>";

				while ($items = mysql_fetch_array($get_items_res)) {
					$item_id = $items['id'];
					$item_title = stripslashes($items['item_title']);
					$item_price = $items['item_price'];

					$display_block .= "<li><a 
					href=\"showitem.php?item_id=$item_id\">$item_title</a>
					</strong> (\$$item_price)";
				}

				$display_block .= "</ul>";
			}
		}
	}
}
?>
<html>
<head>
<title>My Categories</title>
</head>
<body>
<?php print $display_block; ?>
</body>
</html>

Okay, I had to fix a function I had wrong, too, on line 46 (mysql_num_rows instead of mysql_num_res). Thanks for the help, everyone! here is the correct code now:

 

<?php
//connect to database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

$display_block = "<h1>My Store - Item Detail</h1>";

//validate item
$get_item = "select c.cat_title, si.item_title, si.item_price,
si.item_desc, si.item_image from store_items as si left join
store_categories as c on c.id = si.cat_id where si.id = $_GET[item_id]";

$get_item_res = mysql_query($get_item) or die(mysql_error());
$item_id = $_GET['item_id'];

if (mysql_num_rows($get_item_res) < 1) {
//invalid item
$display_block .= "<p><em>Invalid item selection.</em></p>";
} else {
//valid item, get info
$cat_title = strtoupper(stripslashes(
	mysql_result($get_item_res,0,'cat_title')));
$item_title = stripslashes(mysql_result($get_item_res,0, 'item_title'));
$item_price = mysql_result($get_item_res,0, 'item_price');
$item_desc = stripslashes(mysql_result($get_item_res,0, 'item_desc'));
$item_image = mysql_result($get_item_res,0, 'item_image');

//make breadcrumb trail
$display_block .= "<p><strong>You are viewing:</em><br>
<a href=\"seestore.php?cat_id\">$cat_title</a>
	> $item_title</strong></p>

	<table cellpadding=3 cellspacing=3>
	<tr>
	<td valign=middle align=center><img src=\"$item_image\"></td>
	<td valign=middle><p><strong>Description:</strong><br>$item_desc</p>
	<p><strong>Price:</strong> \$$item_price</p>";

	//get colors
	$get_colors = "select item_color from store_item_color where
		item_id = $item_id order by item_color";
	$get_colors_res = mysql_query($get_colors) or die(mysql_error());

	if (mysql_num_rows($get_colors_res) > 0) {

		$display_block .= "<p><strong>Available Colors:</strong><br>";

		while ($colors = mysql_fetch_array($get_colors_res)) {
			$item_color = $colors['item_color'];

			$display_block .= "$item_color<br>";
		}
	}

	//get sizes
	$get_sizes = "select item_size from store_item_size where
		item_id = $item_id order by item_size";
	$get_sizes_res = mysql_query($get_sizes) or die(mysql_error());

	if (mysql_num_rows($get_sizes_res) > 0) {

		$display_block .= "<p><strong>Available Sizes:</strong><br>";

		while ($sizes = mysql_fetch_array($get_sizes_res)) {
			$item_size = $sizes['item_size'];

			$display_block .= "$item_size<br>";
		}
	}

	$display_block .= "
	</td>
	</tr>
	</table>";

}
?>
<html>
<head>
<title>My Store</title>
</head>
<body>
<?php print $display_block; ?>
</body>
</html>

 

Thanks everyone for all the help!

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.