PHP,MySql下拉列表仅显示每个选择的一条记录

时间:2016-05-06 12:33:59

标签: php jquery mysql ajax forms

我是新手,我正在尝试创建一个动态下拉列表,从数据库中检索数据。问题是,它只给我一个下拉项目(在第二个列表上)每个选项在第一个下拉列表。有人请帮忙。 这是代码。

 <?php
    require_once("dbcontroller.php");

    $query ="SELECT * FROM campus";

    ?>
    <html>
    <head>
        <TITLE>Campus and Faculty Select</TITLE>
    <head>
    <style>
            body{width:610px;}
            .frmDronpDown {border: 1px solid #F0F0F0;background-color:#C8EEFD;margin: 2px 0px;padding:40px;}
            .demoInputBox {padding: 10px;border: #F0F0F0 1px solid;border-radius: 4px;background-color: #FFF;width: 50%;}
            .row{padding-bottom:15px;}
    </style>
        <script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
    <script>
        function getcampus_id(val) {
            $.ajax({
            type: "POST",
            url: "get_faculty.php",
            data:'campus_id='+val,
            success: function(data){
                $("#faculty-list").html(data);
            }
            });
        }

        function selectcampus_id(val) {
        $("#search-box").val(val);
        $("#suggesstion-box").hide();
        }
    </script>
    </head>
    <body>
        <div class="frmDronpDown">
            <div class="row">
                <label>Country:</label><br/>
                    <select name="campus" id="campus-list" class="demoInputBox" onChange="getcampus_id(this.value);">
                        <option value="">Select Country</option>
                        <?php
                                    $query ="SELECT * FROM campus";
                                    $results = mysqli_query($con, $query);
                                    //loop
                                    foreach ($results as $campus){
                                ?>
                            <option value="<?php echo $campus["campus_id"]; ?>"> <?php echo $campus["name"]; ?></option>
                            <?php
                                }
                            ?>
                    </select>
            </div>
            <div class="row">
                <label>State:</label><br/>
                    <select name="faculty" id="faculty-list" class="demoInputBox">
                        <option value="">Select State</option>
                    </select>
            </div>
        </div>
    </body>
    </html>

get_faculty.php

    <?php
    require_once("dbcontroller.php");

    if(!empty($_POST["campus_id"])) {
        $campus_id = $_POST["campus_id"];
        $query ="SELECT * FROM faculty WHERE faculty_id = $campus_id";
        $results = mysqli_query($con, $query);
    ?>
        <option value="">Select Campus</option>
    <?php
        foreach($results as $faculty) {
    ?>
        <option value="<?php echo $faculty["faculty_id"]; ?>"><?php echo $faculty["faculty_name"]; ?></option>
    <?php
        }
    }
    ?>

和dbcontroller.php

<?php
$username = "root";
$password = "";
$host = "localhost";
$dbname = "registration";

    $con = mysqli_connect($host, $username, $password) or die("Could not Connect");
    mysqli_select_db($con, $dbname);
?>

3 个答案:

答案 0 :(得分:2)

使用while($row = mysqli_fetch_assoc($result)从数据库中获取所有记录。

<?php
    require_once("dbcontroller.php");

    if(!empty($_POST["campus_id"])) {
        $campus_id = $_POST["campus_id"];
        $query ="SELECT * FROM faculty WHERE faculty_id = $campus_id";
        $results = mysqli_query($con, $query);
    ?>
        <option value="">Select Campus</option>
    <?php
        while($row = mysql_fetch_assoc($results)) {
    ?>
        <option value="<?php echo $row ["faculty_id"]; ?>"><?php echo $row ["faculty_name"]; ?></option>
    <?php
        }
    }
    ?>

答案 1 :(得分:1)

foreach处理一个数组! $result变量不是一个mysqli_result对象

的数组

因此,您需要get每个结果行,然后使用该行数据。为此使用while循环。

当你编写像这样的PHP和HTML来使用

时,它也很有用
while () :
   . . .
endwhile;

语法,因为它可以更容易地查看循环等的开始和结束。否则你会在这类代码中迷失方向。

<?php

error_reporting(E_ALL); 
ini_set('display_errors', 1);

require_once("dbcontroller.php");

if(!empty($_POST["campus_id"])) {
    $campus_id = $_POST["campus_id"];
    $query ="SELECT * FROM faculty WHERE faculty_id = $campus_id";
    $result = mysqli_query($con, $query);

    if ( $result === false ) {
        echo mysqli_error($con);
        exit;
    }

    echo '<option value="">Select Campus</option>';

    while ( $faculty = mysqli_fetch_assoc($result) ) :
        echo '<option value="' . $faculty['faculty_id'] . '">';
        echo $faculty['faculty_name']; 
        echo '</option>';
    endwhile;
}
?>
  

当然,我假设您未展示的代码在此代码之前和之后具有所需的<select....></select>标记

dbcontroller没有看错,但我会做

<?php
$username = "root";
$password = "";
$host = "localhost";
$dbname = "registration";

$link = mysqli_connect($host, $username, $password, $dbname);

if (!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

一旦第一个数据库成功连接到

mysqli_select_db();实际上是切换到第二个数据库

答案 2 :(得分:0)

问题出在这里,

if(!empty($_POST["campus_id"])) {
    $campus_id = $_POST["campus_id"];
    $query ="SELECT * FROM faculty WHERE faculty_id = $campus_id";
    $result = mysqli_query($con, $query);

    if ( $result === false ) {
        echo mysqli_error($con);
        exit;
    }

$query ="SELECT * FROM faculty WHERE faculty_id = $campus_id";

应该是

 $query ="SELECT * FROM faculty WHERE campus_id = $campus_id";