dongmubei7950
dongmubei7950
2014-08-14 09:29

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

hope you can help

I want to have a drop down list that searches my mysql database and shows the first and last names of the users. You can then select any of the names and it will generate the rest of the membership details (i.e. date signed up, last logged in etc).

I have got the dropdown list working fine, but my problem comes with the next step of generating the rest of the details. With my below code it just shows the details of the last user who signed up and does not change when I change the name in the drop down menu.

I'm pretty sure I need Javascript to help get this working but as I am a beginner to PHP/MYSQL any advise toward the right direction for me would be great. Thanks

This is the code which searches my database and puts the first and last name in to a drop down list

$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>';

Again the above code works fine, but the below code is what I am having difficulty with

$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条回答

  • dongzu0742 dongzu0742 7年前

    if you don't want to use AJAX to dynamically load the content, you need a form to submit the data, your problem is that after your first loop, $firstname is the last user loaded into the , to make your code work, you need a form.

    just put the select in a form and add a button

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

    then in mypage.php you need to check if the form is submitted, and show the user details.

    <?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>";
       }
    

    }

    last note, if you don't want the username to appear in the querystring, change form action to post, and get variable using $_POST["name"]

    EDIT: if you want the form to autopost using javascript then add this to ur select definition:

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

    but bear in mind that a lot of users block javascript, thus this will not work.

    点赞 评论 复制链接分享
  • doucong8553 doucong8553 7年前

    Just to complete your options, here you have a full but basic AJAX implementation (using jQuery):

    File select.php (javascript part at the bottom of the file):

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

    And then 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!";
    }
    

    Let me give you some further reading (you said you were new to PHP, I asume the whole web development):

    点赞 评论 复制链接分享
  • doujiyong7604 doujiyong7604 7年前

    Use this code from where user will select the name

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

    Crate one more and give the name alldata.php And use the below code on that page

    <?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>";
    }
    ?>
    
    点赞 评论 复制链接分享
  • duansen6750 duansen6750 7年前

    The best way would be to use AJAX to get the data of the other script. However, another (far less elegant and efficient) way of doing it is while you're looping through the names to put into the select. You can also be using the names to get the info for that name and put it into an array (but don't echo it) until you need it.

    点赞 评论 复制链接分享