选择下拉列表时生成更多MYSQL数据库结果

时间:2014-08-14 09:29:28

标签: javascript php mysql

希望你能帮忙

我想要一个下拉列表来搜索我的mysql数据库,并显示用户的名字和姓氏。然后,您可以选择任何名称,它将生成其余的会员资料(即注册日期,最后登录等)。

我的下拉列表工作正常,但我的问题是生成其余细节的下一步。使用我的下面的代码,它只显示最后一个注册用户的详细信息,并在下拉菜单中更改名称时不会更改。

我很确定我需要使用Javascript来帮助实现这一目标,但由于我是PHP / MYSQL的初学者,任何建议我朝着正确的方向前进都会很棒。感谢

这是搜索我的数据库并将名字和姓氏放入下拉列表的代码

$sql = "SELECT first_name,last_name FROM registration_tbl";
$query = mysqli_query($dbc, $sql);
echo '<select name="name" style="width: 400px">';
while ($row = mysqli_fetch_assoc($query)){
    $firstname = $row['first_name'];
    $lastname = $row['last_name'];
    echo'<option>' . $firstname . " " . $lastname . '</option>';
}
echo '</select> <p>';

再次上面的代码工作正常,但下面的代码是我遇到的问题

$sql = "SELECT * FROM registration_tbl WHERE first_name = '".$firstname."'";
$query = mysqli_query($dbc, $sql);
while($row = mysqli_fetch_array($query)){
    echo "ID: " . " " . $row[0] . "<br>" . 
        "Name: " . $row[1] . " " . $row[2] . "<br>" . 
        "Company: " . $row[3] . "<br> " . 
        "Email: " .$row[4] . "<br> " . 
        "Date of registration: " . $row[6] . "<br> " . 
        "Last login:  " .$row[7] . "<br>" . 
        "Admin/User: " .$row[8] . "<p>";
}

4 个答案:

答案 0 :(得分:1)

如果你不想使用AJAX动态加载内容,你需要一个表单来提交数据,你的问题是你的第一个循环之后,$ firstname是最后一个加载到的用户,来制作你的代码工作,你需要一个表格。

只需将选择放在表单中并添加一个按钮

    <form action="mypage.php" method="get">
    <select name="name" style="width: 400px">
    <?php
    $sql = "SELECT first_name,last_name FROM registration_tbl";
    $query = mysqli_query($dbc, $sql);
    while ($row = mysqli_fetch_assoc($query)){
        $firstname = $row['first_name'];
        $lastname = $row['last_name'];
        echo'<option>' . $firstname . " " . $lastname . '</option>';
    }
    ?>

</select>
<input type="submit" value="Load User Data">
</form>

然后在mypage.php中,您需要检查表单是否已提交,并显示用户详细信息。

<?php
if(isset($_GET["name"]))  {
   //this form is posted, show user details
   //$firstname = $_GET["name"]; <--- SQL Injection enabled here!!
   $firstname = mysqli_real_escape_string($_GET["name"]); // please try to avoid SQL injection!
   //your code continues here
   $sql = "SELECT * FROM registration_tbl WHERE first_name = '".$firstname."'";
   $query = mysqli_query($dbc, $sql);
   while($row = mysqli_fetch_array($query)){
      echo "ID: " . " " . $row[0] . "<br>" . 
        "Name: " . $row[1] . " " . $row[2] . "<br>" . 
        "Company: " . $row[3] . "<br> " . 
        "Email: " .$row[4] . "<br> " . 
        "Date of registration: " . $row[6] . "<br> " . 
        "Last login:  " .$row[7] . "<br>" . 
        "Admin/User: " .$row[8] . "<p>";
   }

}

最后一点,如果您不希望用户名出现在查询字符串中,请将表单操作更改为post,并使用$ _POST [“name”]获取变量

编辑:如果您希望使用javascript将表单自动提交,请将其添加到您的选择定义:

<select name="name" style="width: 400px" onchange="this.form.submit()">

但请记住,很多用户会阻止javascript,因此无法使用。

答案 1 :(得分:0)

最好的方法是使用AJAX来获取其他脚本的数据。然而,另一种(远不那么优雅和有效)的方式是在你通过名称循环进入选择时。您也可以使用这些名称来获取该名称的信息并将其放入一个数组中(但不要回应它),直到您需要它为止。

答案 2 :(得分:0)

使用此代码,用户将从中选择名称

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js">
<script type="text/javascript">
$(document).ready(function()
{

$(".listdata").change(function()
{
var dataString = 'listdata='+ $(this).val();
$.ajax
({
type: "POST",
url: "alldata",
data: dataString,
cache: false,
success: function(html)
{
$(".alldata").html(html);
} 
});

});
});
</script>

<?php
$sql = "SELECT first_name,last_name FROM registration_tbl";
$query = mysqli_query($dbc, $sql);
echo '<select name="name" style="width: 400px" class="listdata">';
while ($row = mysqli_fetch_assoc($query)){
    $firstname = $row['first_name'];
    $lastname = $row['last_name'];
    echo'<option value="'.$firstname.'">' . $firstname . " " . $lastname . '</option>';
}
echo '</select> <p>';
?>
<div class="alldata">
    <!--here your all data will come-->

</div>
<br/><br/>

再加一个箱子,并给出名字alldata.php 并在该页面上使用以下代码

<?php
$sql = "SELECT * FROM registration_tbl WHERE first_name = '".$firstname."'";
$query = mysqli_query($dbc, $sql);
while($row = mysqli_fetch_array($query)){
    echo "ID: " . " " . $row[0] . "<br>" . 
        "Name: " . $row[1] . " " . $row[2] . "<br>" . 
        "Company: " . $row[3] . "<br> " . 
        "Email: " .$row[4] . "<br> " . 
        "Date of registration: " . $row[6] . "<br> " . 
        "Last login:  " .$row[7] . "<br>" . 
        "Admin/User: " .$row[8] . "<p>";
}
?>

答案 3 :(得分:0)

只是为了完成您的选择,这里有一个完整但基本的AJAX实现(使用jQuery):

文件 select.php (文件底部的javascript部分):

<?php
  // Notice that you'll need to get the $dbc from somewhere, maybe require 'db.php'?
  // Retrieve list of people... (notice I've added id column!)
  $sql = "SELECT id, first_name, last_name FROM registration_tbl";
  $query = mysqli_query($dbc, $sql);
  $people = [];
  while ($row = mysqli_fetch_assoc($query)){
    $people[$row['id']] = $row['first_name'] . " " . $row['last_name'];
  }
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>List of people</title>
  <style type="text/css">
    .error {
        color: red;
        box-shadow: 0px 0px 15px 0px rgba(247, 17, 40, 0.44);
    }
    #person-info {
        padding: 10px;
        margin-top: 1em;
    }
  </style>
</head>
<body>
  <h2>People list</h2>
    <label for="person">Please select one person</label> 
    <select name="person" id="people-selection">
        <option value="-1"></option>

    <?php foreach ($people as $person_id => $person_name): ?>
        <option value="<?php echo $person_id ?>"><?php echo $person_name ?></option>
    <?php endforeach; ?>

    </select>
  <div id="person-info"><!-- here we will put person info using AJAX request --></div>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script>
    $(document).ready(function() {
        // We bind our AJAX handler to the onChange event of the select element
        $("#people-selection").on('change', function(e) {
            if ($(this).val() != -1) {  // If we did select some value
                $.ajax({
                    type: "POST",
                    url : "/get_people_info.php",
                    data: { id: $(this).val() },
                })
                  .done(function(data) {
                    $("#person-info")
                        .removeClass("error")  // we don't need the error class here, it's a success response
                        .html(data);
                  })
                  .fail(function(jqXHR, textStatus, errorThrown) {
                    $("#person-info")
                        .addClass("error")      // in order to add some UI for the user
                        .html("Something went wrong!<br><blockquote>" + errorThrown + "</blockquote>");
                  });
            }
            else {
                $("#person-info")
                    .removeClass("error")
                    .html("");
            }
        })
    });
  </script>
</body>
</html>

然后 get_people_info.php

<?php

if ($_SERVER['REQUEST_METHOD'] !== "POST") {
    header('HTTP/1.0 400 Bad request');
    die("Only POST request accedpted!");
}

if (!isset($_POST['id'])) {
    header('HTTP/1.0 400 Bad request');
    die("id parameter is mandatory!");
}

$id = (int) $_POST['id'];

// Notice that you'll need to get the $dbc from somewhere, maybe require 'db.php'?
$sql = "SELECT * FROM registration_tbl WHERE id = ". $id .";";
$query = mysqli_query($dbc, $sql);

if ($row = mysqli_fetch_array($query)){
    echo "<p>ID: " . " " . $row[0] . "<br>" . 
         "Name: " . $row[1] . " " . $row[2] . "<br>" . 
         "Company: " . $row[3] . "<br> " . 
         "Email: " .$row[4] . "<br> " . 
         "Date of registration: " . $row[6] . "<br> " . 
         "Last login:  " .$row[7] . "<br>" . 
         "Admin/User: "  .$row[8] . "</p>";
}
else {
    echo "Person not found!";
}

让我给你一些进一步的阅读(你说你是PHP的新手,我认为整个网络开发):