Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Waring help for Undefined index using PHP/MySQL

Status
Not open for further replies.

Tuck007

Technical User
Apr 29, 2002
105
US
I have been trying to make a small catalog for my website. I really don't know where I've gone wrong here, either with my code or the way I have my MySQL database set up. I really hope someone can help. Here is the bit of code that is bothering me.
Code:
<?php
mysql_select_db(&quot;store&quot;,$db);

$result = mysql_query(&quot;SELECT * FROM catalog WHERE item_view = '1'&quot;,$db);

if (!$result) {
    echo mysql_error();
    exit;
}
?>
<!-- SQL Query results -->
				<?php
				if ($myrow = mysql_fetch_array($result)) {?>
					 <table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; summary=&quot;Query Results&quot; align=&quot;center&quot;>
					 <?php 
					 do {
					 	printf(&quot;<tr><td><img src=\&quot;%s\&quot;></td><td>%s</tr><tr><td align=\&quot;center\&quot;>%s</td><td align=\&quot;center\&quot;><a href=\&quot;%s\&quot;><img src=\&quot;%s\&quot; width=\&quot;60\&quot; height=\&quot;30\&quot;> Go to Ebay Item</a></td></tr>\n&quot;, $myrow[&quot;item_image&quot;], $myrow[&quot;item_desc&quot;], $myrow[&quot;item_name&quot;], $myrow[&quot;ebay_href&quot;], $myrow[&quot;item_ebay&quot;]);
					}
					 while ($myrow = mysql_fetch_row($result));
				?>
				<tr><td colspan=&quot;2&quot; align=&quot;center&quot;><a href=&quot;#top&quot;>Return to Top</a></td></tr>
				</table>
				<?php }
				else{
						 echo &quot;<center>There are no items for sale presently.</center>&quot;;
						 echo &quot;<center><a href=\&quot;#top\&quot;>Return to Top</a></center>&quot;;
						 }
						 ?>
I see the following when brought up:
Warning: Undefined index: item_image in C:\Inetpub\ on line 32

Warning: Undefined index: item_desc in C:\Inetpub\ on line 32

Warning: Undefined index: item_name in C:\Inetpub\ on line 32

Warning: Undefined index: ebay_href in C:\Inetpub\ on line 32

Warning: Undefined index: item_ebay in C:\Inetpub\ on line 32

What can I do to get this up and running? .:TUCK:.
 
The problem is probably that your database setup isn't the same as it is in the script. Check your database and see if those names are right. //Daniel
 
dunno, try this instead..

<?php
mysql_select_db(&quot;store&quot;,$db);

$result = mysql_query(&quot;SELECT * FROM catalog WHERE item_view = '1'&quot;,$db);

echo &quot;<table border=\&quot;1\&quot; cellpadding=\&quot;0\&quot; cellspacing=\&quot;0\&quot; summary=\&quot;Query Results\&quot; align=\&quot;center\&quot;>&quot;;
if(!empty($result)){
while ($myrow = mysql_fetch_array($result)) {

printf(&quot;<tr><td><img src=\&quot;%s\&quot;></td><td>%s</tr><tr><td align=\&quot;center\&quot;>%s</td><td align=\&quot;center\&quot;><a href=\&quot;%s\&quot;><img src=\&quot;%s\&quot; width=\&quot;60\&quot; height=\&quot;30\&quot;> Go to Ebay Item</a></td></tr>\n&quot;, $myrow['item_image'], $myrow['item_desc'], $myrow['item_name'], $myrow['ebay_href'], $myrow['item_ebay']);
}

echo &quot;<tr><td colspan=\&quot;2\&quot; align=\&quot;center\&quot;><a href=\&quot;#top\&quot;>Return to Top</a></td></tr></table>&quot;;

}else{
echo &quot;<center>There are no items for sale presently.</center>&quot;;
echo &quot;<center><a href=\&quot;#top\&quot;>Return to Top</a></center>&quot;;
}
?> ***************************************
Party on, dudes!
[cannon]
 
Holy Moly! That worked great! What was I doing wrong so I don't do it again? .:TUCK:.
 
Oh, I see it now.
In your do...while statement, you are using $myrow = mysql_fetch_row($result) which would give you a numbered index, instead of what you are trying to use.
Your code would work if you changed it to $myrow = mysql_fetch_assoc($result) or $myrow = mysql_fetch_array($result). //Daniel
 
Dunno what you were doing wrong, I wrote that last night after faaaaaaar too much wobbly juice ;-) ***************************************
Party on, dudes!
[cannon]
 
Thanks again for all the info, much helpful. I had a bunch of wobbly juice last night too. Well with that situation fixed, on to the next challenge, still further difficult. .:TUCK:.
 
Actually, I don't know if I can do this, but in this line:
Code:
printf(&quot;<tr><td align=\&quot;center\&quot;><img src=\&quot;%s\&quot; width=\&quot;160\&quot; height=\&quot;160\&quot;></td><td><form><textarea rows=\&quot;4\&quot; cols=\&quot;50\&quot;>%s\n&quot;, $myrow['item_image'], $myrow['item_desc'], $myrow['item_name'], $myrow['ebay_href'], $myrow['item_ebay']);
I'd like to make a href for the image, using the same info from item_image feild. But I'm worried that if I put another
Code:
$myrow['item_image']
that the value from the next row will be supplied. Am I wrong? .:TUCK:.
 
yes,you are wrong ? - im confused now, anyway: you can repeat $myrow['item_image'],$myrow['item_image'],$myrow['item_image'],$myrow['item_image'], ad infinitum .. enjoy :) ***************************************
Party on, dudes!
[cannon]
 
Ok, coolz, so lets say I have a field named item_image, and it contains images/pic001.jpg. I write my script like so:
Code:
printf(&quot;<tr><td><a href=\&quot;%s\&quot;><img src=\&quot;%s\&quot;></a></td><td>The Image %s</td></tr>&quot;, $myrow['item_image'], $myrow['item_image'], $myrow['item_image']);
And after parse, it would look like:
Code:
<tr><td><a href=&quot;images/pic001.jpg&quot;><img src=&quot;images/pic001.jpg&quot;></a></td><td>The Image images/pic001.jpg</td></tr>
? .:TUCK:.
 
If I get confusing, just let me know. I have just recently started using PHP. I have little to no programing experience, but a lot of HTML creation experience. Well, I used to play with BASIC and LOGO back on my Apple II. .:TUCK:.
 
That looks ok to me, just make sure you have a %s for each $row[item] that you use. ***************************************
Party on, dudes!
[cannon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top