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>
8
  • what error do you get? Commented Feb 15, 2013 at 13:00
  • There is no error, just a blank select box which is rather frustrating! Commented Feb 15, 2013 at 13:03
  • i suppose it's not $nurse_name, but nurse_name, in your select query Commented Feb 15, 2013 at 13:04
  • Ive tried changing it, didn't work Commented Feb 15, 2013 at 13:08
  • mysql_ mysqli_ - this is confused gibberish Commented Feb 15, 2013 at 13:14

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

it is still a blank select box. This is frustrating!
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");
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.
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
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>

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.