从下拉列表中选择名称后获取所有mysql行数据

时间:2016-07-20 00:59:36

标签: php mysql database get

下面是我用来填充变量“full_name”的选择下拉列表的php代码。

$sql = "SELECT * FROM Entries";
$result = mysql_query($sql);

echo "<select name='full_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['full_name'] ."'>" . $row['full_name'] ." </option>";
}
echo "</select>";

我希望能够从下拉列表中选择一个名称,并显示该特定行的所有相关数据,以便管理员轻松查看。

基本上

select name (details populate below)

Name: $full_name<br>
DOB: $dob<br>
Work Phone: $work_phone<br>
etc...

也许是这样的?

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  echo "Name :{$row['full_name']}  <br> ".
     "DOB : {$row['dob']} <br> ".
     "Work Phone : {$row['work_phone']} <br> ".
     "--------------------------------<br>";
  }
  echo "Fetched data successfully\n";

我只是不知道如何绑定下拉列表选项以正确显示该人姓名的信息。任何帮助表示赞赏!

由于

更新:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>

    $(document).on("change", "#full_name", function(){

        var elem = $(this),
            full_name = elem.val();

        $(".info").hide(200); /* HIDE ALL OTHER INFORMATION */

        $(".info-"+full_name).show(200); /* SHOW THE INFO OF THE SELECTED FULL NAME */

    });

</script>
</head>

<body>
<select name="full_name" id="full_name">
<?php

$connection = new mysqli("XXXX", "XXXX", "XXXX", "XXXX");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$info = ''; /* WE'LL BE STORING THEIR INFORMATION HERE */

$stmt = $connection->prepare("SELECT full_name, dob, work_phone FROM Entries"); 
if ( false===$stmt ) { die('prepare() failed: ' . htmlspecialchars($mysqli->error)); }
$stmt->execute();
$stmt->bind_result($full_name, $dob, $workphone);
while($stmt->fetch()){

    echo '<option value="'.$full_name.'">'.$fullname.'</option>';
    $info .= '<div class="info info-'.$fullname.'" style="display:none">'.$dob.' - '.$work_phone.'</div>';

}
$stmt->close();

echo '</select>';

echo $info; /* DISPLAY THE INFOs, BUT NOT REALLY BECAUSE THEY ARE HIDDEN */
?>
</body>
</html>

更新:

id
requested_action
full_name
birth_date
sex
work_location
work_phone
hire_date
coverage_choice
network_choice  
plan_choice
dependant_name_1
dependant_relationship_1 
dependant_dob_1
dependant_sex_1 
dependant_name_2 
dependant_relationship_2    
dependant_dob_2 
dependant_sex_2 
dependant_name_3 
dependant_relationship_3
dependant_dob_3 
dependant_sex_3 
dependant_name_4
dependant_relationship_4 
dependant_dob_4 
dependant_sex_4 
spouse_coverage 
employee_enroll 
signature
date_today  
reg_date

UPDATE:找到连接错误 - 现在我得到空白输出

<body> <select name="full_name" id="full_name"> <option value="Customer, Joe"></option><option value="Customer, Susie"></option><option value="Customer, Joe"></option><option value="Customer, Josie, B"></option><option value="Renoir, Thomas"></option><option value="Customer, Joe"></option></select><div class="info info-" style="display:none"> - </div><div class="info info-" style="display:none"> - </div><div class="info info-" style="display:none"> - </div><div class="info info-" style="display:none"> - </div><div class="info info-" style="display:none"> - </div><div class="info info-" style="display:none"> - </div></body> </html>

2 个答案:

答案 0 :(得分:2)

您可以使用Javascript实现此目的。但我们将使用名为jQuery的Javascript库。

哦,不要使用deprecated mysql_*扩展名。我们将改为使用mysqli_*扩展名。

让我们首先在您的id字段中添加<select></select>代码来准备您的HTML。

echo '<select name="full_name" id="full_name">';

然后在你的while循环中,让我们从每一行获取其他信息。

$info = ''; /* WE'LL BE STORING THEIR INFORMATION HERE */

$stmt = $connection->prepare("SELECT id, full_name, birth_date, work_phone FROM Entries"); /* SEE HOW TO ESTABLISH CONNECTION TO YOUR DATABASE USING mysqli AT THE BOTTOM */
$stmt->execute();
$stmt->bind_result($id, $fullname, $dob, $workphone); /* CORRESPONDS TO THE SELECTED COLUMNS FROM YOUR QUERY */
while($stmt->fetch()){

    echo '<option value="'.$id.'">'.$fullname.'</option>';
    $info .= '<div class="info info-'.$id.'" style="display:none">
                  Date of Birth: '.$dob.'<br>
                  Tel. Phone (work): '.$workphone.'
              </div>';

}
$stmt->close();

echo '</select>';

echo $info; /* DISPLAY THE INFOs, BUT NOT REALLY BECAUSE THEY ARE HIDDEN */

然后,让我们创建脚本以显示所选full_name的信息:

<script src="jquery-1.9.1.min.js"></script> <!-- REPLACE JS FILE DEPENDING ON THE VERSION YOU HAVE DOWNLOADED AND THE DIRECTORY WHERE YOU PUT IT -->
<script>

    $(document).on("change", "#full_name", function(){

        var elem = $(this),
            id = elem.val();

        $(".info").hide(200); /* HIDE ALL OTHER INFORMATION */

        $(".info-"+id).show(200); /* SHOW THE INFO OF THE SELECTED FULL NAME */

    });

</script>

这只是一个简单的伎俩。但我认为这比从full_name的每个选项进行Ajax调用更好。

使用mysqli_*扩展程序建立与数据库的连接:

$connection = new mysqli("Host", "User", "Password", "Database");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

答案 1 :(得分:0)

我最后在链接中使用ajax调用来在后台获取删除页面,以避免已经设置并导致问题的http重定向。