Page 1 of 1

not reading form variable

Posted: Thu Nov 06, 2003 11:13 am
by timinator
Hi, I have a search page and results page that is not working. The search form has 2 fields, a dropdown called "state" and a text box called "city" . When I choose a state or city I get an empty table. In the sql statement if I manually type in the state, "AL" for example it will work. Here is the code for the results page: thanks

Code: Select all

<?
$dbuser = "root"; 
$dbserver	= "localhost"; 
$dbpass = "passwd"; 
$dbname	= "NATFD"; 
//******** BEGIN LISTING THE CONTENTS OF  company*********
//CONNECTION STRING
mysql_connect($dbserver, $dbuser, $dbpass)
or die ("UNABLE TO CONNECT TO DATABASE");
mysql_select_db($dbname)
or die ("UNABLE TO SELECT DATABASE");

if ($city=="")
$sql = ("SELECT * FROM company WHERE state='$state'");
elseif ($state=="")
$sql = ("SELECT * FROM company WHERE city='$city'");
else
$sql = ("SELECT * FROM company WHERE city='$city' AND state='$state'");
echo($sql);
$result = mysql_query($sql);
//if ($myrow = mysql_fetch_array($result))
$num_rows = mysql_num_rows($result);
print "<p><a href=javascript:history.back(1)><font size=1>BACK</font></a></p>";
print "<font size=3><strong>$num_rows Records found.<P>";
//Set up the Table
print "<table width=100% border=1>\n";
print "<tr>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>MLA</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>NAME</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>STREET</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>CITY</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>STATE</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>ZIP</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>PHONE</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>FAX</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>WEB SITE</font></td>";
print  "</tr>";
//Get the data into the table
while ($get_info = mysql_fetch_row($result)){ 
print "<tr>\n";
foreach ($get_info as $field) 
print "\t<td>&nbsp<font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";


?>
Edit: Added php tags for easier viewing. --JAM

Posted: Thu Nov 06, 2003 11:26 am
by JAM
viewtopic.php?t=511
Read that first, and see if it is the reason to your problem.

Posted: Thu Nov 06, 2003 11:43 am
by timinator
I tried this but no luck....

$sql = ("SELECT * FROM company WHERE state= $_GET['$state']");

Posted: Thu Nov 06, 2003 11:48 am
by JAM
I was more thinking about this part(s) of the script:

Code: Select all

if ($city=="")
There are also this issue:

Code: Select all

// bad
$sql = ("SELECT * FROM company WHERE state= $_GET['$state']");

// better
$sql = ("SELECT * FROM company WHERE state='$_GET[state]'");

Posted: Thu Nov 06, 2003 12:00 pm
by timinator
Much better - Is it difficult to limit the numer of rows to say "50" and have next and previous links?

thanks

Posted: Thu Nov 06, 2003 12:02 pm
by JAM
Might be interesting:
viewtopic.php?t=13649

Posted: Thu Nov 06, 2003 12:41 pm
by Weirdan
check this also:
http://www.sitepoint.com/article/662/5

I saw a tutorial on how to implement paging using LIMIT, but don't remember where it was...

Posted: Thu Nov 06, 2003 1:40 pm
by timinator
Thanks for the links - while I'm at it I will also need to some of the results columns to have links to detail pages.

thanks