yash
yash

Reputation: 45

Search Function : Error

Im not sure how to correct these errors, can someone help me.

search.php

Notice: Undefined index: submit in C:\wamp\www\i-document\search.php on line 12
Notice: Undefined index: search in C:\wamp\www\i-document\search.php on line 13

After clicking 'search' :

Notice: Undefined variable: x in C:\wamp\www\i-document\search.php on line 39
Notice: Undefined variable: construct in C:\wamp\www\i-document\search.php on line 41

Thankz.

The codes :

<?php

//Get data

$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button)    echo "You didn't submit a keyword."; else {    if (strlen($search)<=1)
      echo "Search term too short";    else    {
      echo "You searched for <b>$search</b><hr size='1'>";

      //connect to database
      mysql_connect("localhost","root","");
      mysql_select_db("idoc");


            //explode search term
            $search_exploded = explode(" ",$search);

            foreach($search_exploded as $search_each);
            {

                //Construct Query

                $x++;
                if ($x==1)
                   $construct .= "file_name LIKE '%$search_each%'";
                else
                   $construct .= " OR file_name LIKE '%$search_each%'";

            }


      $construct = "SELECT * FROM document WHERE $construct";
      //echo out construct

     // $construct = "SELECT * FROM document WHERE $construct";
     $run = mysql_query($construct);

     $foundnum = mysql_num_rows($run);

     if ($foundnum==0)
        echo "No results found";
     else
     {
        echo "$foundnum results found!<p>";

        while ($runrows = mysql_fetch_assoc($run))
        {
         //Get data
         $ref = $runrows['file_ref'];
      $filename = $runrows['file_name'];    $owner = $runrows['owner'];
         $url = $runrows['url'];
         echo "
        <table>   <tr>
    <td> $ref </td>
    <td> $filename </td>
    <td> $owner </td>
    <td><a href='$url'>$url</a></td>
      </tr>    </table>
         ";
        }

     }
  } }
?>

<form id="form1" method="GET" action="search.php">
  <table width="446" height="135" border="1" align="center">
    <tr>
      <td height="31" colspan="2" align="center" valign="middle" bgcolor="#990000">
        <span class="style1 style2">
          Search :
        </span>
      </td>
    </tr>
    <tr>
      <td width="374" height="96" align="center" valign="middle" bgcolor="#990000">
        <span class="style1 style2">
          <label>
            <div align="left">
              Keyword :
              <input name="search" type="text" id="search" size="40" />
            </div>
          </label>
        </span>
        <td width="56" align="center" valign="middle" bgcolor="#990000">
          <div align="left">
            <input type = "submit" name="submit" value="search" />
          </div>
      </td>
    </tr>
  </table>
</form>

Upvotes: 0

Views: 1107

Answers (7)

cypher
cypher

Reputation: 6992

  1. the indexes submit and search are not defined (they're not in URL). Try instead:

    $button = isset($_GET['submit']) ? $_GET['submit'] : false; $search = isset($_GET['search']) ? $_GET['search'] : false;

  2. you NEED to define variables if you don't want to get E_NOTICE on each undefined variable.

    foreach($search_exploded as $search_each) { //defining variables $construct = ''; $x = 0;

                //Construct Query
    
    
    
            $x++;
            if ($x==1)
               $construct .= "file_name LIKE '%$search_each%'";
            else
               $construct .= " OR file_name LIKE '%$search_each%'";
    
    
        }
    

Upvotes: 0

thomaux
thomaux

Reputation: 19718

It means your search index is missing from your url. Verify you are navigating to an url containing '&search='. If you are submitting the search keyword through a form, verify its method attribute is set to 'get

Upvotes: 0

Gumbo
Gumbo

Reputation: 655159

When working with unknown arrays, you should test if an array key exists before reading it. In your case the error messages say that neither submit nor search exist.

Use isset or array_key_exists to check if a array key exists before reading them:

// setting default values
$button = '';
$search = '';

// assigning GET parameter values if existing
if (isset($_GET['submit'])) {
    $button = $_GET['submit'];
}
if (isset($_GET['search'])) {
    $search = $_GET['search'];
}

You can also use the conditional operator cond-expr ? true-expr : false-expr for a more consise notation:

$button = isset($_GET['submit']) ? $_GET['submit'] : '';
$search = isset($_GET['search']) ? $_GET['search'] : '';

Upvotes: 1

Levon Mirzoyan
Levon Mirzoyan

Reputation: 461

These are just Notices, not errors. However if you want to avoid these, you can check if the variable is passed with isset()

Instead of

$button = $_GET['submit'];
if (!$button) 

Use

if (isset($_GET['submit'])) 

Upvotes: 0

fabrik
fabrik

Reputation: 14365

Looks like your error_reporting is set to the maximum level, so you'll get a lot of Notice messages too.

'Undefined index' meaning your $_GET[any] variables are empty before submit.

'Undefined variable' meaning you are using a previously undefined variable ($x++).

It's always a good practice developing at this error_reporting level because you'll get a lot of information about where you should improve your code.

For this example:

$x = 0;
$construct = '';
$button = !empty($_GET['submit']) ? $_GET['submit'] : NULL;
$search = !empty($_GET['search']) ? $_GET['search'] : NULL;

Upvotes: 0

ITroubs
ITroubs

Reputation: 11215

foreach($search_exploded as $search_each);
{

    //Construct Query

    $x++; // this is bad practice because you didn't initialize the $x var before your foreach
    if ($x==1)
        $construct .= "file_name LIKE '%$search_each%'"; //this also "could" cause problems because you didn't define the $construct in the higher scope so that it "might" not be accessable from outside the if statement. BUT sometimes this works
    else
        $construct .= " OR file_name LIKE '%$search_each%'";

}

these were the two "notices" php gave you here:

Notice: Undefined variable: x in C:\wamp\www\i-document\search.php on line 39

Notice: Undefined variable: construct in C:\wamp\www\i-document\search.php on line 41

Upvotes: 0

MatTheCat
MatTheCat

Reputation: 18721

It means $_GET don't contain index 'submit', before form was send. You can disable Notice error render or add a condition using isset().

After you try to increment and concatenate undefined variables.

Upvotes: 0

Related Questions