2013-01-17 62 views
7

我想在選擇框中顯示Mysql表格Filed值。我嘗試了下面的代碼來顯示。 但它通常在回顯功能中顯示指定的字段值,而不是在選擇框中。我不知道我錯在哪裏。在選擇框中顯示Mysql表格字段值

$con = mysql_connect("localhost","root","root"); 
$db = mysql_select_db("Time_sheet",$con); 
$get=mysql_query("SELECT Emp_id FROM Employee"); 
while($row = mysql_fetch_assoc($get)) 
{ 
echo ($row['Emp_id']."<br/>"); 
} 

<html> 
<body> 
<form> 
<select> 
<option value = "<?php echo($row['Emp_id'])?>" ><?php echo($row['Emp_id']) ?></option> 
</select> 
</form> 
</body> 
</html> 

另外的字段值必須按升序順序顯示。如何實現..

+0

,加上'ORDER BY'條款,例如:'選擇emp_id FROM員工ORDER BY EMP_ID ASC' –

+0

感謝。爲什麼字段值不顯示在選擇框中? –

回答

14
<?php 
$con = mysql_connect("localhost","root","root"); 
$db = mysql_select_db("Time_sheet",$con); 
$get=mysql_query("SELECT Emp_id FROM Employee ORDER BY Emp_id ASC"); 
$option = ''; 
while($row = mysql_fetch_assoc($get)) 
{ 
    $option .= '<option value = "'.$row['Emp_id'].'">'.$row['Emp_id'].'</option>'; 
} 
?> 
<html> 
<body> 
<form> 
<select> 
<?php echo $option; ?> 
</select> 
</form> 
</body> 
</html> 

PS:在旁註,請停止使用mysql_*功能。看看this線程的原因。

+0

謝謝asprin這是我想要的 –

+0

不客氣。但請考慮通過我在我的回答 – asprin

+0

提到的線程當然asprin,並再次感謝。 –

2

您可以輕鬆地做到這一點這樣

$con = mysql_connect("localhost","root","root"); 
$db = mysql_select_db("Time_sheet",$con); 
$get=mysql_query("SELECT Emp_id FROM Employee"); 

<html> 
<body> 
<form> 
    <select> 
    <option value="0">Please Select</option> 
     <?php 
      while($row = mysql_fetch_assoc($get)) 
      { 
      ?> 
      <option value = "<?php echo($row['Emp_id'])?>" > 
       <?php echo($row['Emp_id']) ?> 
      </option> 
      <?php 
      }    
     ?> 
    </select> 
</form> 
</body> 
</html> 
+0

謝謝Raheel shan它運作良好。 –

0

你必須使用while循環顯示在選擇框中選擇。試試這個...

$con = mysql_connect("localhost","root","root"); 
$db = mysql_select_db("Time_sheet",$con); 
$get=mysql_query("SELECT Emp_id FROM Employee order by Emp_id"); 

<html> 
<body> 
    <form> 
    <select> 
    <?php 
    while($row = mysql_fetch_assoc($get)) 
    { 
    ?> 
     <option value="<?php echo $row['Emp_id']; ?>"><?php echo $row['Emp_id']; ?></option> 
    <?php 
    } 
    ?> 
    </select> 
    </form> 
</body> 
</html> 
0
<?php 
    $con = mysql_connect("localhost","root","root"); 
    $db = mysql_select_db("Time_sheet",$con); 
    $get=mysql_query("SELECT Emp_id FROM Employee"); 
    ?> 
<html> 
<body> 
    <form> 
     <select> 
     <?php 
      while($row = mysql_fetch_assoc($get)){?> 
       <option value = "<?php echo($row['Emp_id'])?>" ></option> 
       <?php } ?> 
     </select> 
    </form> 
    </body> 

0
<?php 
$con = mysql_connect("localhost","root","root"); 
$db = mysql_select_db("Time_sheet",$con); 
$res=mysql_query("SELECT Emp_id FROM Employee"); 
?> 

<html> 
<body> 
    <form> 
    <select> 
    <?php 
     while ($row = $res->fetch_assoc()) 
     { 
     echo '<option value=" '.$row['id'].' "> '.$row['name'].' </option>'; 
     } 
    ?> 
    </select> 
    <form> 
    </body> 
</html> 
在查詢
相關問題