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!
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.
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.
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.
$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.
<?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.
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.