Jump to content

Notice: Undefined variable


jade24

Recommended Posts

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

$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

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.