I am currently tring to create a form wherein there is three dropdown option boxes and one submit button. All three of the dropdown boxes are populated from the database and I would like the selected options to be included into a new query and printed. This example only shows one dropdown
PHP Code
// Create connection
$con=mysqli_connect('', '', '', '');
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$course_dropdown ="";
$query_course = "SELECT * FROM course";
$result_course = mysqli_query($con,$query_course) or die(mysqli_error());
while($row = mysqli_fetch_assoc($result_course))
{
$course_dropdown .= "<option value='{$row['CourseName']}'{$row['CourseName']} </option>";
}
Above is the code that is used to create the dropdown lists
HTML
<form="index.php" method="post">
<select name="Course"><?php echo $course_dropdown; ?></select>
<input name="button" value="Submit" type="submit">
I am at a loss over what way to proceed, I have tried various different techniques but cannot come up with an answer.
Latest attempt
$course = mysqli_real_escape_string($con, $_POST['Course']);
$query = mysqli_query($con,"SELECT * FROM course_module WHERE CourseName = $course");
this brought an error
Notice: Undefined index: Course in C:\Users\seanin\Desktop\xampp\htdocs\index.php on line 33
So have edited as suggested and stil have an error, may be missing something small.
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$course_dropdown ="";
$query_course = "SELECT * FROM course";
$result_course = mysqli_query($con,$query_course) or die(mysqli_error());
while($row = mysqli_fetch_assoc($result_course))
{
$course_dropdown .= "<option value='{$row['CourseName']}'>{$row['CourseName']}</option>";
}
if ($_POST['button'] == 'Submit') {
$course = mysqli_real_escape_string($con, $_POST['Course']);
$query = mysqli_query($con,"SELECT * FROM course_module WHERE CourseName = $course");
}
Still have this error
Notice: Undefined index: button in C:\Users\seanin\Desktop\xampp\htdocs\index.php on line 30
submit button issue
Nearly done, thanks for all the help so far.
What do I need to do to get the results and print them???
$_POSTvariables into MySQL query's; escape them properly or better, use prepared statements.mysqli_*functions, so Seanin should usemysqli_real_escape_string.