【发布时间】:2015-03-12 09:49:52
【问题描述】:
您好,我正在尝试将表单输入中的用户 ID 值与我的数据库中的记录相匹配。我试图在下拉选择菜单中显示与表单输入中的用户 ID 值匹配的数据列(项目名称)。我不知道我是否正确定义了我的变量或我做错了什么,但看不到我错过了什么。任何帮助是极大的赞赏。谢谢。
<?php
// Create the connection to the database
$con=mysqli_connect("xxx","xxx","xxx","xxx");
// Check if the connection failed
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
if (isset($_POST['userid']))
{
$userid= $_POST['userid'];
$query = "SELECT itemname
FROM seguin_orders
WHERE username = '".($userid)."'";
$result = mysqli_query($con,$query);
}
?>
带下拉菜单
<form action="xxx" method="post" name="form1">
<select name="xxx"><option value="">-- Select One --</option>
<?php
while ($row = mysqli_fetch_assoc($result))
{
echo '<option value =" ' . $row['itemname'] . ' ">' . $row['itemname'] . '</option>';
}
?>
</select>
<input id="input1" name="input1" type="text" />
</br>
<input id="userid" name="userid" type="text" value="demo@gmail.com" readonly="readonly"/>
<button type="submit" value="Submit">Submit</button>
</form>
【问题讨论】:
-
$query = "SELECT itemname FROM seguin_orders WHERE username = '".($userid)."'"; -
我收到解析错误:语法错误,文件意外结束
-
尝试在 WHERE username = '".($userid)."' 后添加分号
-
把
}放在第一个例子中关闭php标签之前。你打开了 if 语句,但没有关闭它 -
在while循环中
}之后不需要;。