使用选择框获取数据库

时间:2016-12-01 11:29:42

标签: php mysql database select interaction

我正在使用bootstrap库测试我在php中的技能,并且我处于死路。

我有两个表,studentscountries

我有一个选择框,我获得了国家(和相关的ID),我希望以一种形式看到该国家的学生。

实际上,我收到了表格,得到了选择,但我无法找到如何互动。

请帮帮我。感谢

我的php的代码

<?php

//Access to BDD
include 'database.php';
?>
<html>

<Head> 
<title>List of students</title> 
<script src="bootstrap/js/bootstrap.js"></script>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
</Head>

<body>

<!-- formulaire de recherche -->
<form class="panel-group form-horizontal" method="get" action="home.php" role="form">
<div class="panel panel-default">
    <div class="panel-body">
        <div class="panel-header">
            <h4>Search</h4>
        </div>
        <div class="col-sm-3">
            <!-- dropdown countries -->

        <select class="form-control" id="select" name="country"> 
            <option value="">country</option>
            <?php
                    $sel_country = "SELECT * FROM countries";
                    $run_country = mysqli_query($conn,$sel_country);
                    while ($rows= mysqli_fetch_assoc($run_country)){
                    echo '<option value='.$rows['id'].'>'.$rows['brand_name'].'</option>';
                    }
            ?>
        </select>
        <br/>
        <br/>



            <button type="submit" class="btn btn-default" id="searchbtn" name="submit">Go</button>
        </div>
    </div>  
</div>
</form>

<?php 

//SQL listing all the students.

$sql = "SELECT * FROM student ORDER by student_id"; //CHOIX NON PLUS!!!
$run_sql = mysqli_query($conn, $sql);
while ($rows = mysqli_fetch_assoc($run_sql)){

        echo '<div class="container">   
                <table class="table table-hover">
                    <tr>
                        <td><h2><a class="btn btn-info" href="detail.php?vo_id='.$rows['student_id'].'">'.$rows ['name'].'</a></h2></td>
                    </tr>
                    <tr>
                        <td>'.$rows ['surname'].'</td>
                    </tr>
                    <tr>
                        <td>'.$rows ['age'].'</td>
                    </tr>
                    <tr>
                        <td>'.$rows ['country'].'</td>
                    </tr>
                </table>    
            </div>
                <br>';
}?>

1 个答案:

答案 0 :(得分:0)

更新现有代码中的以下代码: 假设所有代码都在同一页面上(home.php)

  if(isset($_GET[country]))
    {
    $sql = "SELECT * FROM student WHERE country_id =". $_GET["country"]." ORDER by student_id"; //CHOIX NON PLUS!!!
    $run_sql = mysqli_query($conn, $sql);
    while ($rows = mysqli_fetch_assoc($run_sql)){

            echo '<div class="container">   
                    <table class="table table-hover">
                        <tr>
                            <td><h2><a class="btn btn-info" href="detail.php?vo_id='.$rows['student_id'].'">'.$rows ['name'].'</a></h2></td>
                        </tr>
                        <tr>
                            <td>'.$rows ['surname'].'</td>
                        </tr>
                        <tr>
                            <td>'.$rows ['age'].'</td>
                        </tr>
                        <tr>
                            <td>'.$rows ['country'].'</td>
                        </tr>
                    </table>    
                </div>
                    <br>';
}