2013-02-15 109 views
1

我在填寫表單中的選擇框以顯示來自「護士」表的護士的現有「姓名」時存在困難。誰能告訴我我做錯了什麼?提前致謝!用php mysql填充選擇框

這裏是形式

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

應該填充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> 
+0

你會得到什麼錯誤? – d4rkpr1nc3 2013-02-15 13:00:54

+0

沒有錯誤,只是一個空白的選擇框,這是相當令人沮喪的! – user2075528 2013-02-15 13:03:36

+0

我想它不是'$ nurse_name',而是'nurse_name',在你的選擇查詢中 – d4rkpr1nc3 2013-02-15 13:04:40

回答

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。

+0

它仍然是一個空白的選擇框。這令人沮喪! – user2075528 2013-02-15 13:19:22

+0

這已解決...我只是改變$ fetch_nurse_name = mysql_query(「SELECT DISTINCT $ nurse_name FROM nurse」); to $ fetch_nurse_name = mysql_query(「SELECT DISTINCT Forename FROM nurse」); – user2075528 2013-02-15 13:22:39

+0

爲了幫助您,我們需要查看處理表單的腳本。它位於一個名爲insert.php的文件中。並考慮修改你的原始問題以反映當前的事態。 – Strawberry 2013-02-15 13:32:41

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

檢查是否使用你的MySQL表和列名。有時候,如果你不把這些名字完全寫在你的MySQL表中,那麼它不起作用。假設,

$query = "SELECT nurse_name FROM nurse"; 

在上面的SQL如果MySQL表的名稱是「護士」和列名是「NURSE_NAME」然後寫完全一樣。

$query = "SELECT NURSE_NAME FROM NURSE"; 

所以,你看有時候MySQL表,列名工作區分大小寫。