not reading form variable

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
timinator
Forum Newbie
Posts: 5
Joined: Thu Nov 06, 2003 11:13 am

not reading form variable

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

viewtopic.php?t=511
Read that first, and see if it is the reason to your problem.
timinator
Forum Newbie
Posts: 5
Joined: Thu Nov 06, 2003 11:13 am

Post by timinator »

I tried this but no luck....

$sql = ("SELECT * FROM company WHERE state= $_GET['$state']");
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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]'");
timinator
Forum Newbie
Posts: 5
Joined: Thu Nov 06, 2003 11:13 am

Post by timinator »

Much better - Is it difficult to limit the numer of rows to say "50" and have next and previous links?

thanks
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Might be interesting:
viewtopic.php?t=13649
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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...
timinator
Forum Newbie
Posts: 5
Joined: Thu Nov 06, 2003 11:13 am

Post 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
Post Reply