嗨,我正在尝试将表单输入中的用户 ID 值与数据库中的记录进行匹配。 我正在尝试在下拉选择菜单中显示与表单输入中的用户 ID 值匹配的数据列(itemname)。 我不知道我是否正确定义了我的变量或我做错了什么,但看不到我错过了什么。 任何帮助将不胜感激。 谢谢。
<?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>
==AJAX FORM的解决方案==
订单.php
<?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($_GET['userid']))
{
$userid= $_GET['userid'];
$query = "SELECT itemname
FROM seguin_orders
WHERE username = '".($userid)."'";
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result))
{
echo '<option value =" ' . $row['itemname'] . ' ">' . $row['itemname'] . '</option>';
}
}
?>
带有表单的原始页面
这只是基本和简单的,供您理解。您可以更改它并使其更安全。请阅读评论以了解
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="#">
<div class="pre-dropdown"> <!-- This class is here to hide this mini form after submit with jquery -->
<input type="text" name="userid" id="userid">
<button id="submitId">Submit Id</button>
</div>
<div class="dropdown"> <!-- This is hidden because there is no data, but when userid is submited, this will be visible -->
<select name="xxx" id="dropdown-select">
<option value="">-- Select One --</option>
</select>
</div>
</form>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
$(function(){
$('.dropdown').hide(); // Hide dropdown div when page is loaded, mbetter way will be with css, but it's enough for now
$('#submitId').on('click', function(e){ // Things to do when 'Submit Id' button is clicked
var userid = $('#userid').val(); // Grab user id from text field
e.preventDefault(); // Prevent form from submit, we are submiting form down with ajax.
$.ajax({
url: "orders.php",
data: {
userid: userid
}
}).done(function(data) {
$('.pre-dropdown').hide(); // Hide mini form for user id
$('.dropdown').show(); // show dropdown
$('#dropdown-select').append(data); // append results from orders.php to select
});
});
});
</script>
</body>
</html>
根据需要更改表单。我隐藏pre-dropdown
因为如果用户再次提交userid
,我们将附加结果两次。
你犯了两个错误
您错过了 if 条件结束 } 的结尾,并删除了分号;从 while 循环也缺少字符串结尾。
简而言之,你需要一个好的IDE,它可以告诉你代码中的基本错误
尝试将代码的回显更改为类似这样的内容 ->
echo "<option value ='"". $row['itemname'] ."'">". $row['itemname'] . "</option>";
或者尝试使用 mysqli_fetch_array() 而不是 mysqli_fetch_assoc()
以及更多 将 sql 查询更改为类似这样的东西
$query = "SELECT itemname FROM seguin_orders WHERE username = '$userid'";