使用php mysql填充选择框

时间:2013-02-15 12:46:05

标签: php mysql forms select

我在填写表单中的选择框时遇到困难,以显示“护士”表中护士的现有“名字”。谁能告诉我我做错了什么?提前致谢!

这是表格

 <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>  

QUERY应该填充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>

3 个答案:

答案 0 :(得分:1)

试试这个:

<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>

不要一起使用mysql和mysqli ....你应该使用mysqli或PDO,但不能同时使用它们;) PS:已编辑;)

Saludos。

答案 1 :(得分:0)

如果这与其他答案重复,请道歉,这是使用mysql_语法的答案,尽管您当然应该使用mysqli_或PDO ...

 <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>

答案 2 :(得分:0)

检查您使用的MySQL表和列名称。如果你没有在MySQL表格中准确写出那些名字,有时候它不起作用。假设,

$query = "SELECT nurse_name FROM nurse";

在上面的SQL中,如果MySQL表名为'NURSE'且列名为'NURSE_NAME',那么就像这样写。

$query = "SELECT NURSE_NAME FROM NURSE";

所以,你看看有时MySQL表,列名在区分大小写的情况下工作。