vet911 Posted June 13, 2011 Share Posted June 13, 2011 I need some help with a search box, I getting this error message when I run the program: Undefined index: search in D:\xampp\htdocs\addr\vet_list11.php on line 19 If I type a year in the search box and hit search it works fine, it's just when the script is first run. Any help would be appreciated. Thanks in advance. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <title></title> <head> <link rel="stylesheet" href="./stylesheet/stylesheet1.css" media="screen"> <style type="text/css" media="screen">@import url("./stylesheet/stylesheet2.css");</style> </head> <body> <br><br><br> <center><table cellspacing="5" cellpadding="5" width="600" border="1"> <form action="" method="get"> ie: 1989, 1966, 1954 <input type="text" name="search" /> <input type="submit" value="Search"/> </form> <br><br><br> <?php include "connect.php"; $search = mysql_real_escape_string($_GET['search']); if (!isset($search) OR $search == "") { echo "Enter a search word"; } else echo "<br />"; $query = "SELECT * FROM promo_vet WHERE year LIKE '%$search%' AND photo='1' ORDER BY year ASC" or die(mysql_error()); $result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 3; while($row = mysql_fetch_array($result)) { // make the variables easy to deal with extract($row); // open row if counter is zero if($i == 0) echo "<tr>"; // make sure we have a valid product if($year != "" && $year != null) echo "<td align='center' height='350'><img src=\"$picture\" width='450'><br><FONT COLOR='red'><b>$year</b></FONT><br>$description <br>$boxno<br></td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td>$year</td>"; } ?> </tr> </table></center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/239294-i-need-some-help-with-a-search-box/ Share on other sites More sharing options...
gizmola Posted June 14, 2011 Share Posted June 14, 2011 Your script emits a form. Then in the same script you attempt to read the value of the variable that will come through via form submission: ($_GET['search']). In other words, the way the script is written it is inevitable you will receive this warning. array_key_exists is one technique that you can use to determine whether or not you should enter the processing part of the script that assumes the form was already submitted and there will be a value to look at in $_GET['search']. Quote Link to comment https://forums.phpfreaks.com/topic/239294-i-need-some-help-with-a-search-box/#findComment-1229348 Share on other sites More sharing options...
vet911 Posted June 17, 2011 Author Share Posted June 17, 2011 Thanks for the info, I have found another way of doing this so this thread is done. Quote Link to comment https://forums.phpfreaks.com/topic/239294-i-need-some-help-with-a-search-box/#findComment-1230959 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.