下拉列表值未发送到数据库

时间:2012-01-01 21:53:31

标签: php mysql drop-down-menu

我有一个页面,其中我的数据库中的表的值被拉出并显示在下拉列表中。选择一个值并提交表单后,除下拉列表之外的每个数据都将提交给我的mysql数据库。代码如下:

   <?
$sql="SELECT user_id, firstname FROM Users WHERE role = 'chairperson'";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

    $id=$row["user_id"];
    $thing=$row["firstname"];
    $options.="<OPTION VALUE=\"$id\">".$thing; 
}
?>
<form action="meetingsinserted.php" method="post">
...
<tr>
    <td>      <label for="chairperson">Chairperson:</label>
</td>
    <td><span id="spryselect1">
      <select name="thing" id="chairperson">

<OPTION VALUE=0>
<?=$options?>

     </select>
      <span class="selectRequiredMsg">You Must Choose A Chairperson For This Meeting</span></span></td>
  </tr>
...

meetingsinserted.php页面如下:

     <?php

$title = $_REQUEST['title'];
$chairperson = $_REQUEST['chairperson'];
$secretary = $_REQUEST['secretary'];
$tof = $_REQUEST['tof'];
$occurances = $_REQUEST['occurances'];

$con = mysql_connect("*********","***","****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db('mdb_hj942', $con);

$sql="INSERT INTO Meetings (title, chairperson, secretary, tof, occurances) VALUES ('$title','$chairperson', '$secretary','$tof','$occurances')";

if (!mysql_query($sql,$con))
  {
      echo '<h1>Meeting Has Been Sent To Chairperson For Approval</h1>';
  die('Error: ' . mysql_error());
  }
?>

任何想法的家伙?感谢..

2 个答案:

答案 0 :(得分:1)

<select name="thing" id="chairperson">

应该是

<select name="chairperson" id="chairperson">

另外,请考虑为

添加结束标记
<option>

答案 1 :(得分:0)

首先关闭标记以获得有效的HTML

  <?
$sql="SELECT user_id, firstname FROM Users WHERE role = 'chairperson'";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

    $id=$row["user_id"];
    $thing=$row["firstname"];
    $options.="<OPTION VALUE=\"$id\">".$thing."</option>"; 
}
?>
<form action="meetingsinserted.php" method="post">
...
<tr>
    <td>      <label for="chairperson">Chairperson:</label>
</td>
    <td><span id="spryselect1">
      <select name="thing" id="chairperson">

<OPTION VALUE=0>
<?=$options?>

     </select>
      <span class="selectRequiredMsg">You Must Choose A Chairperson For This Meeting</span></span></td>
  </tr>

你试图通过id值属性获取值,这是不正确的,你需要从name属性值中调用它

 <?php

$title = $_REQUEST['title'];
$chairperson = $_REQUEST['thing']; //here was your problem 
$secretary = $_REQUEST['secretary'];
$tof = $_REQUEST['tof'];
$occurances = $_REQUEST['occurances'];

$con = mysql_connect("*********","***","****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db('mdb_hj942', $con);

$sql="INSERT INTO Meetings (title, chairperson, secretary, tof, occurances) VALUES ('$title','$chairperson', '$secretary','$tof','$occurances')";

if (!mysql_query($sql,$con))
  {
      echo '<h1>Meeting Has Been Sent To Chairperson For Approval</h1>';
  die('Error: ' . mysql_error());
  }
?>