dongmubei7950 2014-08-14 09:29
浏览 37
已采纳

选择下拉列表时生成更多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 2014-08-14 09:53
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码