Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

to search a mysql database by the field surname

Status
Not open for further replies.

biscuitboy

Technical User
Dec 11, 2004
22
0
0
GB
Hi all,

Im trying to create a simple script to search a mysql database and search by the surname of the entries. Ufortunatly I have tried this script in the browser and doesnt seem to work.

I have created a connect.php script which I know works and so I know that isnt the problem.

Could someone have a quick look at it as im sure its a simple thing that I have missed but its driving me mad.

Best regards


<?php # Scrtipt 6.6 - Searchbysurname.php


$page_title = 'Search By Surname';

if (isset($_POST['submit'])) { // Handle the form.

$message = NULL;
if (empty($_POST['surname'])) {
$ln = FALSE;
$message .= '<p>You forgot to enter a surname!</p>';
} else {
$ln = $_POST['surname'];
}

if ($ln) { //if everthings is ok.
require_once ('connect.php'); // Connect to the db.
//Make the Query
$query = "SELECT first_name, surname FROM Residents WHERE surname= '$ln'";
$result = mysql_query ($query); // run the query

if ($result) {


echo '<table align="centre"
cellspacing="2" cellpadding="2">
<tr><td align="left"><b>Name</b>
</td></tr>
';
//fetch and print all the records.
while ($row = mysql_fetch_array($result)) {
echo "<tr><td align=\"left\">" .
stripslashes($row[0]) . "</td>
<td align=\"left\">$row[1]</td>
</tr>\n";
}

echo '</table>';
mysql_free_result ($result); //Free up the resources.

} else { // if it did not run ok
echo '<p>There are currently no registered residents.</p>';
}

mysql_close(); // close the database connection
} else {
$message .= '<p>Please try again.</p>';
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
<fieldset><legend>Enter a Surname below:</legend>
<p><b>Last Name:</b> <input type="text" name="surname" size="30" maxlength="30" value="<?php if (isset($_POST['surname'])) echo $_POST['surname']; ?>" /></p>
<div align="centre"><input type="submit" name"submit" value="Search" /></div>
</form><!-- end of form -->
 
what error messages do you get? or if no error messages then add
Code:
error_reporting(E_ALL);
and rerun.

if still no error messages, in what way is the script not working?
 
Hiya,

Currently when I run the script nothing appears on the screen?

Andy ideas?

Regards
 
am assuming you put the error reporting directive at the top of the code?

if so, then the next step is to add in lots of debugging code.

1. add
Code:
print_r($_POST);
as the second line of the code
2. add
Code:
echo $query;
as the line after you have built the sql code
3. change the query line to
Code:
$result = mysql_query ($query) or die (mysql_error()); // run the query

this should give us some output allowing a simpler debug process.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top