Jump to content

I need some help with a search box


vet911

Recommended Posts

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>

Link to comment
Share on other sites

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'].

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.