如何将表单输入中的用户 ID 值与数据库中的记录相匹配?
How can I match a user id value from a form input to records in my database?
您好,我正在尝试将表单输入中的用户 ID 值与数据库中的记录相匹配。我正在尝试在下拉菜单 select 中显示一列(项目名称)数据,该菜单与表单输入中的用户 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>
你犯了两个错误
您错过了 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'";
==AJAX 表格的解决方案==
orders.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
,我们将附加结果两次。
您好,我正在尝试将表单输入中的用户 ID 值与数据库中的记录相匹配。我正在尝试在下拉菜单 select 中显示一列(项目名称)数据,该菜单与表单输入中的用户 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>
你犯了两个错误
您错过了 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'";
==AJAX 表格的解决方案==
orders.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
,我们将附加结果两次。