简体   繁体   中英

populate a select box with php mysql

I'm having difficulty populating a select box within a form to display existing "forenames" of nurses from the "Nurses" table. Could anyone tell me what Im doing wrong? Thanks in advance!

Here is the form

 <form method="post" action="insert.php"> 
 <br>
 <tr><td align="left"><strong>Nurse Information</strong></td>
 </td>
 <tr> 
 <td>nurse_name</td>
       <td><select name="valuelist">
    <option value="valuelist" name="nurse_name" value='<?php echo $nurse_name; ?>'></option>

 </select></td>
 <tr>  

The QUERY which should populate the nurse_forename:

<html><head><title>Connect to Database</title></head><body>
<font size="4">Query gets Forename of nurse</font>
<br><br><font size="4">Choose a name</font><br><br> 

<form action="insert.php" method="post">
<select name="valuelist">;
<?php
$value=$_POST ["valuelist"];
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
 mysql_select_db("a&e", $con) or die('Could not select database.');

$fetch_nurse_name = mysql_query("SELECT DISTINCT $nurse_name FROM nurse");
$result = mysqli_query($con, $query) or die("Invalid query");

while($throw_nurse_name = mysqli_fetch_array($fetch_nurse_name)) {
echo '<option   value=\"'.$nurse_name['nurse_name'].'">'.$throw_nurse_name['nurse_name'].'</option>';
}
echo "</select>";

mysqli_close($con);
 ?>
<input type="submit" value="Submit">
</form></body></html>

Try this:

<html><head><title>Connect to Database</title></head><body>
 <font size="4">Query gets Forename of nurse</font>
 <br><br><font size="4">Choose a name</font><br><br> 

 <form action="insert.php" method="post">
<select name="valuelist">;
<?php
$value=$_POST ["valuelist"];
$con = mysql_connect("localhost","root","") or die('Could not connect:'.mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');

$fetch_nurse_name = mysql_query("SELECT DISTINCT Forename FROM nurse");


while($throw_nurse_name = mysql_fetch_array($fetch_nurse_name)) {
echo '<option   value=\"'.$throw_nurse_name[0].'">'.$throw_nurse_name[0].'</option>';
}
echo "</select>";


?>
<input type="submit" value="Submit">
</form></body></html>

Dont use mysql and mysqli together....you should use mysqli or PDO, but not a mix of both ;) PS: Edited ;)

Saludos.

Apologies if this duplicates other answers, Here's an answer using mysql_ syntax although you should of course be using mysqli_ or PDO for this...

 <form action="insert.php" method="post">
 <select name="valuelist">;
 <?php

 //path to connection statements
 include('path/to/connection/stateme.nts'); 

 //fetch nurse name
 $query = "SELECT nurse_name FROM nurse;";

 $result = mysql_query($query) or die(mysql_error()); //note: use mysql_error() for development only

 //print results
 while($row = mysql_fetch_assoc($result)) {
 echo '<option   value=\"'.$row['nurse_name'].'">'.$row['nurse_name'].'</option>';
 }
 echo "</select>";

  ?>
 <input type="submit" value="Submit">
 </form>

Check your MySQL table and column name that you using. Sometimes it does not work if you don't write those names exactly which in your MySQL table. suppose,

$query = "SELECT nurse_name FROM nurse";

in above SQL if MySQL table name is 'NURSE' and column name is 'NURSE_NAME' then write exactly like this.

$query = "SELECT NURSE_NAME FROM NURSE";

So, you look that sometime MySQL table, column name work in case sensitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM