3

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>
user2075528
  • 55
  • 2
  • 2
  • 5

3 Answers3

1

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.

Hackerman
  • 12,139
  • 2
  • 34
  • 45
  • it is still a blank select box. This is frustrating! – user2075528 Feb 15 '13 at 13:19
  • This has been resolved...I simply changed $fetch_nurse_name = mysql_query("SELECT DISTINCT $nurse_name FROM nurse"); to $fetch_nurse_name = mysql_query("SELECT DISTINCT Forename FROM nurse"); – user2075528 Feb 15 '13 at 13:22
  • To help you with that, we'd need to see the script that processes the form. It's in a file called insert.php. And consider amending your original question to reflect the current state of affairs. – Strawberry Feb 15 '13 at 13:32
  • sorry..another query. Okay I got the select box to populate from the values within the database but it does not appear on the actual form after submit – user2075528 Feb 15 '13 at 13:33
0

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>
Strawberry
  • 33,750
  • 13
  • 40
  • 57
0

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.

Arno
  • 4,994
  • 3
  • 39
  • 63
MMReza
  • 115
  • 1
  • 9