如何从select标记获取值到数据库

时间:2016-08-18 10:06:22

标签: php

我可以将表单发布到数据库中但是我只获得了0作为select标记的值,我如何获得数据库中每个选择选项的值?

<select class="span6" name="test_name">
<?php
include ('include/connect.php');

$query1 = "SELECT test_name FROM mst_test ORDER BY test_name ASC";
$result = mysql_query($query1);

while(list($test_name)=mysql_fetch_array($result))
{
  echo "<option value ='$test_name' selected>$test_name</option>";
}
?>
</select>

2 个答案:

答案 0 :(得分:0)

<select class="span6" name="test_name">
  <?php
  include ('include/connect.php');
  $query1 = "SELECT test_name FROM mst_test ORDER BY test_name ASC";
  $result = mysql_query($query1);
  while ($row = mysql_fetch_array($result)) {?>
    <option value ="<?php echo $row['test_name'];?>"><?php echo $row['test_name'];?></option>
  <?php }?>
</select>

NOTE: [The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead ]

答案 1 :(得分:0)

<select class="span6" name="test_name">
<option value="">Please select</option>
  <?php
  include ('include/connect.php');
  $query1 = "SELECT test_name FROM mst_test ORDER BY test_name ASC";
  $result = mysql_query($query1);
  while ($test_name = mysql_fetch_assoc($result)) {

    echo "<option value='{$test_name['test_name']}'>{$test_name['test_name']}</option>";
  }
  ?>
</select>

我强烈建议您切换到MySQLiPDO