jade24 Posted February 10, 2013 Share Posted February 10, 2013 I am working on the search function of my project. And I cant debug this error on my search page: Notice: Undefined variable: itemcode in C:\xampp\htdocs\sample3\search.php on line 18 Notice: Undefined variable: query in C:\xampp\htdocs\sample3\search.php on line 18 ----This is the content of my search page: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[url="http://www.w3.org/TR/html4/strict.dtd%22>"]http://www.w3.org/TR...l4/strict.dtd">[/url] <html> <?php // Search Variables include('view.php'); //Create Query $query ["SELECT * FROM shoe WHERE itemcode LIKE '%".$itemcode."%' "] or die(mysql_error()); //$mysql_query = "SELECT * FROM shoe WHERE itemcode='%$itemcode%'" or die (mysql_error()); $result = mysql_query($query) or die (mysql_error()); $num=mysql_numrows($result); mysql_close($dbConn); echo "<b><center>Database Output</center></b><br><br>"; $i=0; while ($i < $num) { $itemcode=mysql_result($result,$i,"itemcode"); $itemdesc=mysql_result($result,$i,"itemdesc"); $color=mysql_result($result,$i,"color"); $size=mysql_result($result,$i,"size"); $price=mysql_result($result,$i,"price"); $qty=mysql_result($result,$i,"qty"); $i++; } ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <input type="hidden" name="itemcode" value="<?php echo $itemcode; ?>"/> <table border="1"> <tr> <td colspan="2"><b><font color='Red'>Inventory Records </font></b></td> </tr> <tr> <td width="150"><b><font color='#663300'>ITEM CODE<em>*</em></font></b></td> <td><label> <?php echo "$itemcode"; ?> </label></td> </tr> <td width="150"><b><font color='#663300'>ITEM DESCRIPTION<em>*</em></font></b></td> <td><label> <?php echo "$itemdesc"; ?> </label></td> </tr> <tr> <td width="150"><b><font color='#663300'>COLOR<em>*</em></font></b></td> <td><label> <?php echo "$color"; ?> </label></td> </tr> <tr> <td width="150"><b><font color='#663300'>SIZE<em>*</em></font></b></td> <td> <?php echo "$size"; ?> </td> </tr> <tr> <td width="150"><b><font color='#663300'>PRICE<em>*</em></font></b></td> <td> <?php echo "$price"; ?> </td> </tr> <tr> <td width="150"><b><font color='#663300'>QUANTITY<em>*</em></font></b></td> <td> <?php echo "$qty"; ?> </td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/274285-notice-undefined-variable/ Share on other sites More sharing options...
Zane Posted February 10, 2013 Share Posted February 10, 2013 $query ["SELECT * FROM shoe WHERE itemcode LIKE '%".$itemcode."%' "] or die(mysql_error()) You are missing the equals sign. Also, why do you have this in square brackets? This part while ($i < $num) { $itemcode=mysql_result($result,$i,"itemcode"); $itemdesc=mysql_result($result,$i,"itemdesc"); $color=mysql_result($result,$i,"color"); $size=mysql_result($result,$i,"size"); $price=mysql_result($result,$i,"price"); $qty=mysql_result($result,$i,"qty"); $i++; } ....is better off like this while ($r = mysql_fetch_assoc($result)) { $itemcode=$r['itemcode']; ... ... ... ... } And finally,. your code will only display ONE result, which will be the very last one in your $result. The displaying HTML needs to be within the while loop Link to comment https://forums.phpfreaks.com/topic/274285-notice-undefined-variable/#findComment-1411481 Share on other sites More sharing options...
jade24 Posted February 10, 2013 Author Share Posted February 10, 2013 I tried your suggestion. But the result displays only the last row or the last inputted data. And also, I still have this error: Notice: Undefined variable: itemcode in C:\xampp\htdocs\sample3\search.php on line 18 Link to comment https://forums.phpfreaks.com/topic/274285-notice-undefined-variable/#findComment-1411483 Share on other sites More sharing options...
Barand Posted February 10, 2013 Share Posted February 10, 2013 That is because $itemcode has never had a value assigned to it before you attempt to use it Link to comment https://forums.phpfreaks.com/topic/274285-notice-undefined-variable/#findComment-1411485 Share on other sites More sharing options...
jade24 Posted February 10, 2013 Author Share Posted February 10, 2013 I've figured it out. The query should be: $query = "SELECT * FROM shoe WHERE itemcode LIKE '%".$_POST['search']."%'" or die (mysql_error()); Link to comment https://forums.phpfreaks.com/topic/274285-notice-undefined-variable/#findComment-1411486 Share on other sites More sharing options...
Barand Posted February 10, 2013 Share Posted February 10, 2013 You should read this tutorial on security, section 3 is relevant in this case http://www.phpfreaks.com/tutorial/php-security Link to comment https://forums.phpfreaks.com/topic/274285-notice-undefined-variable/#findComment-1411488 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.