isign4jc Posted June 19, 2009 Share Posted June 19, 2009 I have been going crazy for the past few days trying to figure out what seem to be such a simple code. What I want is a website visitor to be able to choose a church name from a drop down menu and when that church is chosen (and they press "submit"), the details (church name, phone, zip code, city name) will be displayed. I have been messing with this code and as of right now when you click "submit", all the records from the database show instead of just the one(s) chosen for that particular church's name. <?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++; } } ?> Link to comment https://forums.phpfreaks.com/topic/162960-php-mysql-drop-down-menu-to-display-database-results/ Share on other sites More sharing options...
isign4jc Posted June 19, 2009 Author Share Posted June 19, 2009 i tried to modify my text on the top because i meant to say that i wanted the drop down to be by city, not by church name. Link to comment https://forums.phpfreaks.com/topic/162960-php-mysql-drop-down-menu-to-display-database-results/#findComment-859876 Share on other sites More sharing options...
SetToLoki Posted June 19, 2009 Share Posted June 19, 2009 <option value=$row[city]>$row[city]</a></option> closed <a> without opening it anywhere also looking for a field named user_id I belibve you are looking for city <option value=$row[city]>$row[city]</option> ????? $query = "SELECT * FROM dbtry WHERE $dropdown='$dropdown'" or die (mysql_error()); your WHERE claus is looking for a vlaue and not a table name. $query = "SELECT * FROM dbtry WHERE city='$dropdown'" or die (mysql_error()); Link to comment https://forums.phpfreaks.com/topic/162960-php-mysql-drop-down-menu-to-display-database-results/#findComment-859895 Share on other sites More sharing options...
SetToLoki Posted June 19, 2009 Share Posted June 19, 2009 also $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++; } while ($row = mysql_fetch_assoc($results)) { echo $row[church_name].', '.$row[$city].', '.$row[zip].', '.$row[phone].'<br />'; } Link to comment https://forums.phpfreaks.com/topic/162960-php-mysql-drop-down-menu-to-display-database-results/#findComment-859898 Share on other sites More sharing options...
SetToLoki Posted June 19, 2009 Share Posted June 19, 2009 my code my be rusty not really done php in a long while and it came from top of my head. so if you get erros in the stuff I altered then it will be more than likely punctuation or somethng lol hope it helps a little without your databse I can't test not got mysql server anywhere I can use just now Link to comment https://forums.phpfreaks.com/topic/162960-php-mysql-drop-down-menu-to-display-database-results/#findComment-859899 Share on other sites More sharing options...
SetToLoki Posted June 19, 2009 Share Posted June 19, 2009 my code my be rusty not really done php in a long while and it came from top of my head. so if you get erros in the stuff I altered then it will be more than likely punctuation or somethng lol hope it helps a little without your databse I can't test not got mysql server anywhere I can use just now <?php require_once ('../../../../mysql_connect2.php'); function PrintChurchSelect () { $result = mysql_query("SELECT * FROM dbtry ORDER BY city ASC") or die(mysql_error()); while ($result) { echo "<option value=$row[city]>$row[city]</a></option>"; } } function ShowForm () { echo sprintf('<form action="%s" method="post">',$_SERVER['PHP_SELF']); echo '<select name=dropdown>'; PrintChurchSelect (); echo '</select>'; echo '<input type="Submit" value="Submit" name="Submit">'; } if (!isset($_POST['Submit'])) { ShowForm (); } else { // form submitted // set server access variables $dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown']); //Create Query $sql = "SELECT * FROM dbtry WHERE city='$dropdown'"; echo "<div align="center" style="font-weight:bold">Database Output</div><br />"; while ($row = mysql_fetch_assoc) { echo $row[church_name].', '.$row[$city].', '.$row[zip].', '.$row[phone].'<br />'; } } ?> Link to comment https://forums.phpfreaks.com/topic/162960-php-mysql-drop-down-menu-to-display-database-results/#findComment-859909 Share on other sites More sharing options...
SetToLoki Posted June 19, 2009 Share Posted June 19, 2009 my code my be rusty not really done php in a long while and it came from top of my head. so if you get erros in the stuff I altered then it will be more than likely punctuation or somethng lol hope it helps a little without your databse I can't test not got mysql server anywhere I can use just now <?php require_once ('../../../../mysql_connect2.php'); function PrintChurchSelect () { $result = mysql_query("SELECT * FROM dbtry ORDER BY city ASC") or die(mysql_error()); while ($result) { echo "<option value=$row[city]>$row[city]</a></option>"; } } function ShowForm () { echo sprintf('<form action="%s" method="post">',$_SERVER['PHP_SELF']); echo '<select name=dropdown>'; PrintChurchSelect (); echo '</select>'; echo '<input type="Submit" value="Submit" name="Submit">'; } if (!isset($_POST['Submit'])) { ShowForm (); } else { // form submitted // set server access variables $dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown']); //Create Query $sql = "SELECT * FROM dbtry WHERE city='$dropdown'"; echo "<div align="center" style="font-weight:bold">Database Output</div><br />"; while ($row = mysql_fetch_assoc) { echo $row[church_name].', '.$row[$city].', '.$row[zip].', '.$row[phone].'<br />'; } } ?> OOPS! echo '<div align="center" style="font-weight:bold">Database Output</div><br />'; Link to comment https://forums.phpfreaks.com/topic/162960-php-mysql-drop-down-menu-to-display-database-results/#findComment-859917 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.