Guillaum Artichaud Guillaum Artichaud - 7 days ago 7
MySQL Question

Getting database fetched with select box

I'm testing my skills in php using bootstrap librairies and i'm in a dead end.

I have 2 tables, 1 about students and the other is about countries.

I got a select box where I get the country (and the ID associated) and I want to see the students from that country in a form.

Actually, I got the form, got the select but i can't find how to interact together.

Please, help me. Thanks

the code of my 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>';
}?>





Answer

Update below code in existing: Assuming that all code is on same page (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>';
}