我有一张包含ID
和DOC_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是资源,布尔给定
答案 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];
}