$Three3 Posted March 11, 2010 Share Posted March 11, 2010 Hi everyone, I have a small problem. I am trying to get a list of names (the names of people are coming from my database) to show up in a option list html drop down. I have done this before plenty of times but have no idea why this is not working. I guess I just need a fresh pair of eyes to look at it. Any help is greatly appreciated. Thanks in advance. Oh yeah, by the way, I am not getting any errors. It is just that nothing shows up in the drop down list. I have typed the same query in the phpMyAdmin panel and it works perfectly so I know it is not the query itself. <?php //Check to see if the form has been submitted if (isset($_POST['submitted'])) { //Connect to the database require_once('/mysql_connect.php') ; //Create the query $query = "SELECT * FROM students WHERE student_id = {$_POST['student_name']}" ; $result = mysqli_query($dbc, $query) ; $num = mysqli_num_rows($result) ; if ($num >= 1) { //Query was successfull //Store all infomation in array //Display the students information $row = mysqli_fetch_array($result, MYSQLI_BOTH) ; echo '<p>Student ID: <b>' . $row['student_id'] . '</b></p>' ; echo '<p>Student Name: <b>' . $row['first_name'] . ' ' . $row['last_name'] . '</b></p>' ; echo '<p>Student Phone #: <b>' . $row['phone'] . '</b></p>' ; echo '<p>Student Address: <b>' . $row['address'] . '<br />' . $row['city'] . ', ' . $row['state'] . ' ' . $row['postal_code'] . '</b></p>' ; } else { //There was an error with the query echo '<p>There was an error with the query. Error # 1. The mysqli_error() is: ' . mysqli_error($dbc) ; } } else { //The form has not been submitted //Display the form echo ' <fieldset><legend>Please select a student from the drop down list.</legend> <form action="home.php" method="post"> <select name="student_name" size="1">' ; //Create the query $query = "SELECT student_id, first_name, last_name FROM students ORDER BY last_name" ; $result = mysqli_query($dbc, $query) ; $num = mysqli_num_rows($result) ; if ($num >= 1) { //Query was successfull //Fetch all of the results while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { echo '<option value="' . $row['student_id'] . '">' . $row['last_name'] . ', ' . $row['first_name'] . '</option>' ; } } else { //There was an error with the query echo '<p>There was an error with the query. Error # 2. The mysqli_error() is: ' . mysqli_error($dbc) ; } echo ' </select> <p><input name="submit" type="submit" value="Submit"></p> <p><input name="submitted" type="hidden" value="true"></p> </form>' ; } ?> Link to comment https://forums.phpfreaks.com/topic/194947-need-help-with-showing-names-in-a-html-drop-down-menu-from-a-database/ Share on other sites More sharing options...
TeddyKiller Posted March 12, 2010 Share Posted March 12, 2010 I haven't read through your code. I am tired. In the database table you'll have a row called... "names" for example. - You'd do a query to get these names. - You'd then do a while eg: <select name="names"> <option value="">-Select-</option> <?php $query = mysql_query("SELECT names FROM TABLE mytable"); if(mysql_num_rows($query) > 0){ while($row = mysql_fetch_array($query)){ echo '<option>'.$row['names'].'</option>'; } } else { echo '<option value="">No Names</option>'; } ?> </select> Link to comment https://forums.phpfreaks.com/topic/194947-need-help-with-showing-names-in-a-html-drop-down-menu-from-a-database/#findComment-1025007 Share on other sites More sharing options...
bateman Posted March 12, 2010 Share Posted March 12, 2010 Try this (cleared all the echo's where not needed) <?php //Check to see if the form has been submitted if (isset($_POST['submitted'])) { //Connect to the database require_once('/mysql_connect.php') ; //Create the query $query = "SELECT * FROM students WHERE student_id = {$_POST['student_name']}" ; $result = mysqli_query($dbc, $query) ; $num = mysqli_num_rows($result) ; if ($num >= 1) { //Query was successfull //Store all infomation in array //Display the students information $row = mysqli_fetch_array($result, MYSQLI_BOTH) ; ?> <p>Student ID: <b><?=$row['student_id'];?></b></p>' ; <p>Student Name: <b><?=$row['first_name'];?> <?=$row['last_name'];?></b></p> <p>Student Phone #: <b><?=$row['phone'];?></b></p> <p>Student Address: <b><?=$row['address'];?><br /><?=$row['city'];?>,<?=$row['state'] . ' ' . $row['postal_code'];?></b></p>' ; <?} else { //There was an error with the query ?> <p>There was an error with the query. Error # 1. The mysqli_error() is: <? mysqli_error($dbc); } } else { //The form has not been submitted //Display the form ?> <fieldset><legend>Please select a student from the drop down list.</legend> <form action="home.php" method="post"> <select name="student_name" size="1">' ; <?//Create the query $query = "SELECT student_id, first_name, last_name FROM students ORDER BY last_name" ; $result = mysqli_query($dbc, $query) ; $num = mysqli_num_rows($result) ; if ($num >= 1) { //Query was successfull //Fetch all of the results while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { ?> <option value="<?=$row['student_id'];?>"><?=$row['last_name'];?>,<?=$row['first_name'];?></option>' ; <?} } else { //There was an error with the query ?><p>There was an error with the query. Error # 2. The mysqli_error() is: <? mysqli_error($dbc); } ?> </select> <p><input name="submit" type="submit" value="Submit"></p> <p><input name="submitted" type="hidden" value="true"></p> </form> <?} ?> Link to comment https://forums.phpfreaks.com/topic/194947-need-help-with-showing-names-in-a-html-drop-down-menu-from-a-database/#findComment-1025027 Share on other sites More sharing options...
TeddyKiller Posted March 12, 2010 Share Posted March 12, 2010 With that code too, replace the "<?" with "<?php" Link to comment https://forums.phpfreaks.com/topic/194947-need-help-with-showing-names-in-a-html-drop-down-menu-from-a-database/#findComment-1025149 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.