我在使用 PHP 生成的 SELECT 列表向表中插入一行时遇到了问题。
该页面有一个“?orgid=#”$_GET 变量并设置了 $orgid:
//set variables
$orgid = $_GET['orgid'];
然后该页面运行适当的查询,该查询将填充 SELECT 列表的 OPTIONS:
//select info from members table
$usrqry = 'SELECT * FROM members WHERE assign = 1 ORDER BY usr ASC';
$usrres = mysql_query($usrqry);
接下来,页面检查表单是否通过 $_POST 提交:
if($_POST['submit']=='Assign') {
$sql = 'INSERT INTO profile (id, usr_id, org_id) VALUES (NULL, '.$_POST['userid'].', '.$orgid.')';
$res = mysql_query($sql);
}
而且,这是页面上的表格:
<form action="" method="post">
<label>Select User:</label>
<select name="the_name">
<?php while($row = mysql_fetch_array($usrres)) { ?>
<option name="userid" value="<?=$row['id'];?>"><?=$row['usr'];?></option>
<?php } ?>
<input type="submit" name="submit" value="Assign">
</form>
当我输入问题时,我偶然发现了我的解决方案:
原始代码:
<form action="" method="post">
<label>Select User:</label>
<select name="the_name">
<?php while($row = mysql_fetch_array($usrres)) { ?>
<option name="userid" value="<?=$row['id'];?>"><?=$row['usr'];?></option>
<?php } ?>
<input type="submit" name="submit" value="Assign">
</form>
更新代码:
<form action="" method="post">
<label>Select User:</label>
<select name="userid">
<?php while($row = mysql_fetch_array($usrres)) { ?>
<option value="<?=$row['id'];?>"><?=$row['usr'];?></option>
<?php } ?>
<input type="submit" name="submit" value="Assign">
</form>