Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Script to search MySQL Database- Need help...


PHP Script to search MySQL Database- Need help...

Reply
  #1 (permalink)  
Old 07-21-09, 06:18 AM
andharbaahar andharbaahar is offline
New Member
 
Join Date: Jul 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question 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:

Code:
<html>

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

<body bgcolor=#ffffff>

<h2>Search</h2>

<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>
</Select>

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

</body>

</html>
And the code for the studentsearch-exec.php page is:

Code:

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

<?php

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!!!";
exit;
}

// 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
$anymatches=mysql_num_rows($data);
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;
//}
?> 


</body>
</html>
But when I try to execute this, and try out a search term, this is the window that I get:

Quote:
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
Reply With Quote
  #2 (permalink)  
Old 07-21-09, 07:59 AM
therocket954's Avatar
therocket954 therocket954 is offline
Community Liaison
 
Join Date: Jul 2007
Location: Michigan, USA
Posts: 334
Thanks: 2
Thanked 8 Times in 8 Posts
Just taking a quick look.... in your studentsearch-exec.php (before your first IF statement you haven't initialized the $find variable, which is why your IF statement is always returning your result.

You have to tell your script what $find is... like this:

PHP Code:

$find $_POST['find']; 

__________________
--Eric Allison
Twitter: http://www.twitter.com/Eric_Allison
Reply With Quote
Reply

Bookmarks

Tags
database, php, script, search, sql


Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting multiple records from MYSQL database using PHP sujata_ghosh PHP 5 06-04-09 03:07 AM
search text (database) send to php and play sas Flash & ActionScript 0 06-21-08 09:07 PM
Best FREE newsletter script in PHP + MySQL PurpleCow Script Requests 0 08-29-06 01:39 AM
a script to search a mysql database larneman Script Requests 0 07-01-05 10:25 AM
PHP MySQL Database Search Help brimultimedia PHP 0 03-17-05 12:51 PM


All times are GMT -5. The time now is 08:42 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.