如何将表单输入中的用户标识值与数据库中的记录进行匹配?

时间:2015-01-14 09:03:14

标签: php forms mysqli

您好我正在尝试将表单上的输入中的用户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 WITH DROPDOWN

<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>

3 个答案:

答案 0 :(得分:2)

== 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,我们会将结果追加两次。

答案 1 :(得分:0)

你犯了两个错误

如果条件结束,你会错过结束},并删除分号;来自while循环也缺少字符串结尾。

简而言之,您需要一个好的IDE,可以告诉您代码中的基本错误

答案 2 :(得分:0)

尝试将代码的回显更改为类似的内容 - &gt;

echo "<option value =\"". $row['itemname'] ."\">". $row['itemname'] . "</option>";

或尝试使用mysqli_fetch_array()而不是mysqli_fetch_assoc()

以及更多将您的SQL查询更改为此类

$query = "SELECT itemname FROM seguin_orders WHERE username = '$userid'";