[SOLVED] PHP MySQL Help - Extreme NOOB Alert
Posted: Sun Mar 06, 2005 4:15 am
Ok, first off, i'll keep this simple. I'm not a total retard but i seem to be stuck. All i want to do is have a web page with 1 field that when i enter a value into it, it becomes my search query. I have everything setup fine with LAMP and i want to display a web page that has a box for the first 3 letters/numbers of a Postal Code to be used as the search query. When i enter a value, it just retuns my entire database. If i actually go into the php file and add the value manually, the search works fine. Man, i need help. I did search, but i can't seem to find a solution and it's going on a week now.
Here are the two files. One is the html file that has the search box and the other is the php script. Any help would be great.
the html file:
the php script:
Now, if i take ....
and change the $postal_code variable to one i want to use, the value works - like
I get results from that query just fine. Again, i only want to use the first 3 values of the postal code. Now I am a total noob on this stuff so go easy. I really can't see what i am doing wrong. Also, I wasn't sure if this should go into the php/mysql forums or this one.
Here are the two files. One is the html file that has the search box and the other is the php script. Any help would be great.
the html file:
Code: Select all
<html>
<head>
<title>Database Search</title></head>
<BODY BGCOLOR="#FFFFFF" onUnload="nothing()"> <div align="center">
Enter the first three letters of the Postal Code here:<p>
<form action="query.php" method="post">
Postal Code: <input type="text" name="postal_code"><br>
<input type="submit" value="submit" name="submit">
</form>
<BODY>
<html>Code: Select all
#query.php
<?
// connection information
$hostName = "localhost";
$userName = "bob";
$password = "password";
$dbName = "Database";
// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");
mysql_select_db($dbName) or die("Unable to select database $dbName");
// Select all the fields in all the records
$query = "SELECT * FROM Database WHERE Postal_Code LIKE '$postal_code%'";
$result = mysql_query($query);
// Determine the number of employees
$number = mysql_numrows($result);
if ($number == 0) {
print "Sorry, there were no records matching those criteria";
} else {
// Print the employee names
for($i=0; $i<$number; $i++){
$name = mysql_result($result,$i,"name");
$address = mysql_result($result,$i, "address");
$city = mysql_result($result, $i, "city");
$province = mysql_result($result, $i, "province");
$postal_code = mysql_result($result, $i, "postal_code");
print "$name, $address, $city,
$province, $postal_code<br>";
}
}
// Close the database connection
mysql_close();
?>Code: Select all
$query = "SELECT * FROM Database WHERE Postal_Code LIKE '$postal_code%'";Code: Select all
$query = "SELECT * FROM Database WHERE Postal_Code LIKE 'P4R%'";