Notice: Undefined variable: results in
C:\xampp\htdocs\01test\index.php on line 31
According to the above error the statement on line 31 is,
while($row = mysqli_fetch_array($results))
So according to the error, the problem is in the following statement.
$results = mysqli_query($dblink, $query) or die (mysqli_error());
The above statement is inside your if-else
statement. Since you have declared that variable and assigned inside the if
statement you will get that error because it can't be accessed from the outside. So I suggest you declare the variable $results
outside the if
statement. But hold on you have another error.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,
null given in C:\xampp\htdocs\01test\index.php on line 31
$results = mysqli_query($dblink, $query) or die (mysqli_error());
From the following statement you are parsing the above variable,
while($row = mysqli_fetch_array($results))
So your $results
is in the if-else
statement. But you have called it from outside the if
statement in mysqli_fetch_array()
. Your if condition is checking that you submit the post or not. Since you are not submitted and your following code
while($row = mysqli_fetch_array($results))
is outside the if condition $results
give you null
value. In brief your code while($row = mysqli_fetch_array($results))
is calling $results
if you submitted the post or not. So now you can understand what is the issue.
So finally, your code should be like this,
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "abc";
$dblink = mysqli_connect($servername,$username,$password,$database);
if(isset($_POST['submit'])) {
if($_POST['select']=='all_records') {
$query = "SELECT Kod_Dorm FROM dorm";
} elseif($_POST['select']=='A302') {
$query = "SELECT Kod_Dorm FROM dorm WHERE Kod_Dorm = 'A302'";
} elseif($_POST['select']=='A303') {
$query = "SELECT Kod_Dorm FROM dorm WHERE Kod_Dorm = 'A303'";
}
$results = mysqli_query($dblink, $query) or die (mysqli_error());
echo "<table>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($results)) { //Creates a loop to loop through results
echo "<tr><td>".$row['Kod_Dorm']."</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
}
?>
By putting the following code inside the if statement will solve your problem because if you only submit the form then it will fetch values. Hope this helps you!
echo "<table>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($results)) { //Creates a loop to loop through results
echo "<tr><td>".$row['Kod_Dorm']."</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML