PHP Script to search MySQL Database- Need Help

Hi All…

I’m working on a website, and one of the pages is an option to search for a particular student. I (modified and) used the following free script I found online, but for some reason, it won’t work. I’ve got two pages- one is the Search form (studentsearch-form.php), and the other is the Search execution code (studentsearch-exec.php).

The code for studentsearch-form.php is:


<title>Searching for a student...</title>

<body bgcolor=#ffffff>


<form name="search" method="post" action="studentsearch-exec.php">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="firstname">First Name</option>
<Option VALUE="lastname">Last Name</option>
<Option VALUE="idnumber">ID Number</option>

<input type="submit" name="search" value="Search" />



And the code for the studentsearch-exec.php page is:

<head><title>Searching for a student...</title>
<body bgcolor=#ffffff>


echo "<h2>Search Results:</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
echo "<p>You forgot to enter a search term!!!";

// Otherwise we connect to our Database
mysql_connect("localhost", "root", "{MyPassword}") or die(mysql_error());
mysql_select_db("databasename") or die(mysql_error());

// We perform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM tablename WHERE upper($field) LIKE'%$find%'");

//And we display the results
while($result = mysql_fetch_array( $data ))
echo $result['firstname'];
echo " ";
echo $result['lastname'];
echo "<br>";
echo $result['idnumber'];
echo "<br>";
echo "<br>";

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
if ($anymatches == 0)
echo "Sorry, but we can not find an entry to match your query...<br><br>";

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;


But when I try to execute this, and try out a search term, this is the window that I get:

Search Results:

Notice: Undefined variable: find in C:\wamp\www\SWD\studentsearch-exec.php on line 15

You forgot to enter a search term!!!

I’ve been sitting on this for a few days now, and I’m falling behind schedule. Any help would be appreciated… also, if, as an alternative, you know of a Search script that works, I would appreciate it if you would provide a link to it or share it with me (if it’s free, that is…).

Thanks in advance… :).

Tunçay Sanli

at studentsearch-exec.php.

I would also avoid the use of inline PHP within HTML as you have there.

I always put an include at the top of HTML pages, and then put as much PHP as I can in the include file (abit like a code file). Obviously when you are displaying information on screen you need code within HTML.

Magic!!! :). That was perfect, blubb… works great now. Thank you so much… :).

And littlened, thanks for the tip. I’ll check online for the syntax to use “include”, and try to incorporate it into the code…

Cheers :)…

Tunçay Sanli