Page 1 of 1
Displaying results using a php/mysql drop down menu
Posted: Fri Jun 19, 2009 6:29 pm
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++;
}
}
?>
Re: Displaying results using a php/mysql drop down menu
Posted: Fri Jun 19, 2009 7:27 pm
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.
Re: Displaying results using a php/mysql drop down menu
Posted: Fri Jun 19, 2009 9:22 pm
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.
Re: Displaying results using a php/mysql drop down menu
Posted: Fri Jun 19, 2009 9:52 pm
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.
Re: Displaying results using a php/mysql drop down menu
Posted: Sat Jun 20, 2009 1:18 pm
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++;
}
}
?>
Re: Displaying results using a php/mysql drop down menu
Posted: Mon Jun 22, 2009 1:25 pm
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?
Re: Displaying results using a php/mysql drop down menu
Posted: Mon Jun 22, 2009 10:40 pm
by neomulemi6
Is there a reason why you're using mysql_result instead of fetching an array?
Re: Displaying results using a php/mysql drop down menu
Posted: Tue Jun 23, 2009 1:00 am
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.
Re: Displaying results using a php/mysql drop down menu
Posted: Tue Jun 23, 2009 1:17 pm
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!!