Making MySQL SELECT query base on selection list
Posted: Sat Dec 21, 2013 1:09 am
I am trying to make query to a MySQL database to return all columns in a table based on the selected element in a selection list. The query appears to be successful although it is not returning any columns in the query. Any recommendations would be appreciated.
Here is what the selection list looks like:
Here is the code to generate a new query:
Here is what the selection list looks like:
Code: Select all
echo "Select a course name<br><br>";
echo "<select name='course' id='course'>";
// Assigns the $result1 to $row1(array) and loops through the array.
while ($row1 = mysqli_fetch_array($result1)) {
if ($row1['code'] == $_POST['course']) {
// Applies the second value of the $row array to the drop down list and assigns the first value of the array as its value.
echo '<option value="' . $row1['code'] . '" selected="selected">' . $row1['course_name'] . '</option>';
} else {
echo '<option value="' . $row1['code'] . '" >' . $row1['course_name'] . '</option>';
}
} // Ends first while loop.
// Finish off the select control.
echo "</select><br><br>";Code: Select all
$course = mysqli_real_escape_string($link2, $_POST['course']);
// Makes a selection query for the course_name and code columns in the courses table.
$result3 = mysqli_query($link2, "SELECT * FROM courses WHERE course_name = '{$course}'");
if($result3 === FALSE){
die(mysqli_error());
}
// Assigns the contents of the array generated from the $result3 query to the name $row3.
$row3 = mysqli_fetch_array($result3, MYSQL_ASSOC);
print_r($row3);
//DEBUG CODE echo "Query 3 successful<br>";