当用户在下拉列表

时间:2016-11-18 06:27:57

标签: php html dropdown

我有一张包含IDDOC_DESC的表格。当用户从下拉菜单中选择DOC_DESC并提交时,应打印该特定行的ID

我写了以下代码:

<?php    
mysql_connect("localhost","root","");
mysql_select_db("dbtest");
?>
<form name="form1" method="post" action="">
<?
$sql=mysql_query("SELECT ID,DOC_DESC FROM document_type_master");
  if(mysql_num_rows($sql)){
    $select= '<select name="select">';
    while($rs=mysql_fetch_array($sql)){
       $select.='<option value="'.$rs['ID'].'">'.$rs['DOC_DESC'].'</option>';
  }
}
$select.='</select>';
echo $select;
?>
<input type="submit" value="Submit" name="submit">
</form>

<?php
if(!isset($_POST['submit']))
{
    echo " "; //ID should be printed here
}
?>

我也遇到错误:     mysql_num_rows()期望参数1是资源,布尔给定

2 个答案:

答案 0 :(得分:0)

您没有将连接实例传递给mysql_query()。因此结果资源不适用于mysql_num_rows()函数。

$link = mysql_connect("localhost","root","");
mysql_select_db("dbtest",$link);
?>
<form name="form1" method="post" action="">
<?
$sql=mysql_query("SELECT ID,DOC_DESC FROM document_type_master",$link);
  if(mysql_num_rows($sql)){
    $select= '<select name="select">';
    while($rs=mysql_fetch_array($sql)){
       $select.='<option value="'.$rs['ID'].'">'.$rs['DOC_DESC'].'</option>';
  }
}
$select.='</select>';
echo $select;
?>
<input type="submit" value="Submit" name="submit">
</form>

答案 1 :(得分:0)

J ust do as -

$sql=mysql_query("SELECT ID,DOC_DESC FROM document_type_master");  
echo "<select name='select'>";
     while ($row=mysql_fetch_array($sql))
          {
             echo "<option value='$row['ID']'>".$row['DOC_DESC']."</option>";
          }
echo "</select";

if(isset($_POST['submit']))
{
   echo $_POST['select];
}