Displaying results using a php/mysql drop down menu

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
isign4jc
Forum Newbie
Posts: 10
Joined: Tue Jan 13, 2009 2:03 pm

Displaying results using a php/mysql drop down menu

Post by isign4jc »

I need my code to display the info that is in the database corresponding to what is chosen by the drop down menu. My code is below. Right now, when you choose a city, it displays all the records in the database, instead of just showing the info related to that city.

Code: Select all

<?php
 
error_reporting(E_ALL);
if (!isset($_POST['Submit'])) {
// form not submitted
?>
 
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<?php
require_once ('../../../../mysql_connect2.php'); // Connect to the db.
 
$res=mysql_query("SELECT * FROM dbtry order by city") or die(mysql_error());
echo "<select name=dropdown>";
while($row=mysql_fetch_assoc($res)) {
 
 
echo "<option value=$row[user_id]>$row[city]</a></option>";
}
echo "</select>";
?>
 
 
<input type="Submit" value="Submit" name="Submit"> 
</form>
 
<?php
}
 
else {
 
// form submitted
// set server access variables
        require_once ('../../../../mysql_connect2.php'); // Connect to the db.
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown']);
 
// Open Connection
 
 
//Create Query
 
$query = "SELECT * FROM dbtry WHERE $dropdown='$dropdown'" or die (mysql_error());
 
$result = mysql_query($query) or die (mysql_error());
 
$num=mysql_numrows($result);
 
 
echo "<b><center>Database Output</center></b><br><br>";
 
$i=0;
while ($i < $num) {
 
$church_name=mysql_result($result,$i,"church_name");
$city=mysql_result($result,$i,"city");
$zip=mysql_result($result,$i,"zip");
$p=mysql_result($result,$i,"phone");
 
 
echo "$church_name, $city, $zip, $p  ";
 
$i++;
 
}
}
?>
Last edited by califdon on Fri Jun 19, 2009 7:21 pm, edited 1 time in total.
Reason: Moderator added proper tags to display PHP
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Displaying results using a php/mysql drop down menu

Post by califdon »

Try echoing your $query value before you run the query. You shouldn't be using or die(... as part of the string assignment. That's very useful on the next line, where you are invoking a mysql function, but not on a string assignment.
isign4jc
Forum Newbie
Posts: 10
Joined: Tue Jan 13, 2009 2:03 pm

Re: Displaying results using a php/mysql drop down menu

Post by isign4jc »

I apologize for being dumb, but could you be a little more specific? Maybe mention what line to put it at? I gave it a shot, but I know that I did it wrong. Sorry.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Displaying results using a php/mysql drop down menu

Post by califdon »

Instead of this:

Code: Select all

$query = "SELECT * FROM dbtry WHERE $dropdown='$dropdown'" or die (mysql_error());
 
$result = mysql_query($query) or die (mysql_error());
do this:

Code: Select all

$query = "SELECT * FROM dbtry WHERE dropdown='$dropdown'";
 echo $query;  //  debug -- remove after you have it working
$result = mysql_query($query) or die (mysql_error());
I just noticed that you have an extra $ in your query line, which of course will make it fail, so that's probably the cause of your problem. But do what I said, anyway. This is how you must learn to diagnose your own problems. You have to be able to see what the script is doing, and echoing values that you think you know what they contain is a valuable exercise.
isign4jc
Forum Newbie
Posts: 10
Joined: Tue Jan 13, 2009 2:03 pm

Re: Displaying results using a php/mysql drop down menu

Post by isign4jc »

Thank you soooooooo much! I appreciate you taking the time to help me out. I have script working now to exactly how I want it. Here it is below:

Code: Select all

<?php
 
error_reporting(E_ALL);
if (!isset($_POST['Submit'])) {
// form not submitted
?>
 
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<?php
require_once ('../../../../mysql_connect2.php'); // Connect to the db.
 
$res=mysql_query("SELECT DISTINCT city FROM dbtry order by city") or die(mysql_error());
echo "<select name=dropdown>";
while($row=mysql_fetch_assoc($res)) {
 
 
echo "<option value=$row[city]>$row[city]</a></option>";
}
echo "</select>";
?>
 
 
<input type="Submit" value="Submit" name="Submit"> 
</form>
 
<?php
}
 
else {
 
// form submitted
// set server access variables
        require_once ('../../../../mysql_connect2.php'); // Connect to the db.
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown']);
 
// Open Connection
 
 
//Create Query
 
    $query = "SELECT * FROM dbtry WHERE city='$dropdown'";
 echo $query;  //  debug -- remove after you have it working
    $result = mysql_query($query) or die (mysql_error());
$num=mysql_numrows($result);
 
 
echo "<b><center>Database Output</center></b><br><br>";
 
$i=0;
while ($i < $num) {
 
$church_name=mysql_result($result,$i,"church_name");
$city=mysql_result($result,$i,"city");
$zip=mysql_result($result,$i,"zip");
$p=mysql_result($result,$i,"phone");
 
 
echo "$church_name, $city, $zip, $p  ";
 
$i++;
 
}
}
?>
Last edited by Benjamin on Tue Jun 23, 2009 10:16 am, edited 1 time in total.
Reason: Changed code type from text to php.
isign4jc
Forum Newbie
Posts: 10
Joined: Tue Jan 13, 2009 2:03 pm

Re: Displaying results using a php/mysql drop down menu

Post by isign4jc »

After being sooo happy that this was finally working I realized a glitch in my code that I am not sure why is happening. If my city has two words (for instance "Newport News"), then it displays a blank page on the results page.

Here is what it looks like: http://www.saltedwebsites.com/demo/desi ... final2.php

Do you know why this would happen?
neomulemi6
Forum Newbie
Posts: 3
Joined: Sat Jun 20, 2009 9:56 pm

Re: Displaying results using a php/mysql drop down menu

Post by neomulemi6 »

Is there a reason why you're using mysql_result instead of fetching an array?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Displaying results using a php/mysql drop down menu

Post by McInfo »

isign4jc wrote:If my city has two words (for instance "Newport News"), then it displays a blank page on the results page.
With this code

Code: Select all

echo "<option value=$row[city]>$row[city]</a></option>";
if $row['city'] is "Newport News", the HTML will be

Code: Select all

<option value=Newport News>Newport News</a></option>
meaning that the option tag has two attributes: "value" with a value of "Newport" and "News" with no value. Also, there is an orphaned </a> tag.

To fix this, add quotes like this

Code: Select all

echo "<option value='$row[city]'>$row[city]</option>";
or like this

Code: Select all

echo '<option value="'.$row['city'].'">'.$row['city'].'</option>';
or like this

Code: Select all

printf('<option value="%1$s">%1$s</option>', $row['city']);
or some other format.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:33 pm, edited 1 time in total.
isign4jc
Forum Newbie
Posts: 10
Joined: Tue Jan 13, 2009 2:03 pm

Re: Displaying results using a php/mysql drop down menu

Post by isign4jc »

Thankyou, thankyou, thankyou! It's so nice to be able to ask a question in a forum and receive a pertinent, quick response!!!! Thanks again!!
Post Reply