Jump to content

Need Help With Showing Names in a HTML Drop Down Menu From a Database


$Three3

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.