[SOLVED] PHP MySQL Help - Extreme NOOB Alert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Phorem
Forum Newbie
Posts: 20
Joined: Sun Mar 06, 2005 3:58 am

[SOLVED] PHP MySQL Help - Extreme NOOB Alert

Post by Phorem »

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:

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>
the php script:

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) &#123;
   print "Sorry, there were no records matching those criteria";
&#125; else &#123;
   // Print the employee names
   for($i=0; $i<$number; $i++)&#123;
    $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>";
   &#125;
&#125;

// Close the database connection
mysql_close();

?>
Now, if i take ....

Code: Select all

$query = "SELECT * FROM Database WHERE Postal_Code LIKE '$postal_code%'";
and change the $postal_code variable to one i want to use, the value works - like

Code: Select all

$query = "SELECT * FROM Database WHERE Postal_Code LIKE 'P4R%'";
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.
Last edited by Phorem on Sun Mar 06, 2005 2:58 pm, edited 1 time in total.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

'Retard' sounds abit harsh. :lol: Everyone starts somewhere...

Code: Select all

echo substr($_POST&#1111;'postal_code'],0,3);
1. $_POST contains everything that you... well... POST... ( method="post" in the <form> )

2. substr() to get a certain num of letters out of the submitted info.

Hope this helps some.
Phorem
Forum Newbie
Posts: 20
Joined: Sun Mar 06, 2005 3:58 am

Post by Phorem »

Thanks for the tip JAM, but i have no clue where i would use that command you just posted for me. I am still new to this stuff. I have no idea how to actually apply that code to my php file. Thanks for the help.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Place it after <? in your query.php page .. That is the line of code thats grabs the data posted from the search form ..

Note: For your starting php tag you are best to use <?php ..

..
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

hi

i think that you can first use JAM's proposal for a check to see what tha three first letters in $_POST are.

If they turn out to be fin you can use them in following

Code: Select all

$postal_code = substr($_POST&#1111;'postal_code'],0,3);
and then use the variable in your search string.

another thing is that you use the call mysql_numrows(), and i hav only seen it used as mysql_num_rows(). Don't know if that will make a difference :wink:

regards

/thallish
Phorem
Forum Newbie
Posts: 20
Joined: Sun Mar 06, 2005 3:58 am

Post by Phorem »

Well, thanks to all 3 of you, it works!! First, thanks JAM for the code. Secondly, thanks Jim_Bo for sending me in the right direction and lastly, thanks thallish for the additional code to make it all work. So it was literaly the combination of all three of you. Thanks Again.

Solved:

query.php

Code: Select all

<?php
echo substr($_POST&#1111;'postal_code'],0,3);
$postal_code = substr($_POST&#1111;'postal_code'],0,3);
// 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 Table 1 WHERE Postal_Code LIKE '%$postal_code%'";

$result = mysql_query($query);

// Determine the number of employees
$number = mysql_num_rows($result);

if ($number == 0) &#123;
   print "Sorry, there were no records matching those criteria";
&#125; else &#123;
   // Print the employee names
   for($i=0; $i<$number; $i++)&#123;
    $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>";
   &#125;
&#125;

// Close the database connection
mysql_close();

?>
With the html search page i posted above and this query.php script, it creates an easy, one field query web page that can be addopted to any use now. Thanks again!
Post Reply