-3

I have a problem with my PHP code saying that "Notice: Undefined index" I am sure its very simple, since I am a beginner i am not getting well what is wrong exactly so please help me.

This is my form

<form action="search.php" method="post">
Search by Name From Database:<br>
<input type="text" name="search" id="snacks"/><br>
<input type="submit" value="Search"/>
</form>

this is php code

<?php
 $search_term = $_POST["search"];
 mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
 mysql_select_db("mydata");

if(empty($search_term))
{
    echo ("<b>Search Not Found. .</b>");
}
else
{
$result1= mysql_query( "SELECT * FROM loc WHERE name LIKE '%$search_term%' " ) 
or die("SELECT Error: ".mysql_error()); 

$count= mysql_num_rows($result1);

if ($count == 0)
{
echo "<fieldset><b>No Results Found for Search Query '$search_term'</b></fieldset>";
}
else
 {
 echo "<table border='0' font color='red' bgcolor='lightblue'>
 <tr align='left' >
 <th><font color='green'>ID</font></th>
 <th><font color='green'>Name</font></th>
 <th><font color='green'>Salary</font></th>
 <th><font color='green'>Location</font></th>
 <th><font color='green'>Contact</font></th>
 <th><font color='green'>Occupation</font></th>
 </tr>" ;
 while ($row = mysql_fetch_array($result1)){ 

 echo "<div align=center></div>";
  "
   <tr bgcolor='lightgrey'>
   <td>$row[0]</td>
     <td>$row[1]</td>
    <td>$row[2]</td>
    <td>$row[3]</td>
    <td>$row[4]</td>
    <td>$row[5]</td>
    </tr>";


    } 

   print "</table>\n"; 
 }
 }

?> 
Phil
  • 157,677
  • 23
  • 242
  • 245
  • What's the rest of the notice message? It will tell you what index it is that it can't find, and on what line of your code it occurs. – Mark Parnell Apr 29 '13 at 06:09
  • My guess, *Undefined index "search" on line 1* – Phil Apr 29 '13 at 06:10
  • can you please give fill details on which line you are getting this error, give complete notice message – Hiren Soni Apr 29 '13 at 06:10
  • 2
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Apr 29 '13 at 06:10
  • 1
    `` / *I am a beginner* — Please find some more up to date resources to learn from. It's been about 15 years since it was a good idea to use the `` element. – Quentin Apr 29 '13 at 06:11
  • 1
    Having syntax issue in PHP - inside while condition – suresh gopal Apr 29 '13 at 06:12
  • as beginner you can use whatever makes a sense honesly but I will take you advice to find up to date resource but for now what is the solution. its undefined index of variable search.$search_term = $_POST["search"]; – user2276709 Apr 29 '13 at 06:22
  • this is the error'''Notice: Undefined index: search in C:\inetpub\wwwroot\~search.php on line 10 Call Stack: 0.0011 332688 1. {main}() C:\inetpub\wwwroot\~search.php:0 – user2276709 Apr 29 '13 at 06:28

2 Answers2

2

"Notice: Undefined Index" means you are trying to access a variable in an array that doesn't exist or is miss-spelled. Look for these:

$row[5]

or these:

$_POST["search"]

One of these values isn't set (you can check that with isset() ), or is miss-spelled.

Borniet
  • 3,544
  • 4
  • 24
  • 33
0

use isset() function in your variables

isset() function in PHP determines whether a variable is set and is not NULL. It returns a Boolean value, that is, if the variable is set it will return true and if the variable value is null it will return false. More details on this function can be found in PHP Manual.

Þaw
  • 2,047
  • 4
  • 22
  • 39