I have a PHP script for a website I am building, and it is communicating with a MySQL database have set up. I want to display the courses in one of my database tables as a list of checkboxes.
After a lot of trial and error, I managed to do that. It is in the while loop below, using mysqli_fetch_array().
Now I am stuck on how to actually get the checked values and save only the checked courses to another table in my database (courses taken). As you can see, I select from the DB and output all checkbox lines in one loop. But I need to also check after I output all of them which ones the user has selected.
I tried saving all the $row values to a row array and accessing that twice, but it did not work at all (tons of errors). I want to know the best way to go about this.
Here's my code:
<form action='' method='POST'>
<input type="submit" name="no_courses_button" value="No"><br>
</form>
<form action='' method='POST'>
<input type="submit" name="yes_courses_button" value="Yes"><br><br>
</form>
<?php
if(isset($_POST['no_courses_button'])){ //if the user has no courses taken so far
echo "Thank you. Please press NEXT below to proceed."."<br>"; #no need to insert in DB
}
if(isset($_POST['yes_courses_button'])){ //if the user has taken courses taken so far
session_start();
$netID = $_SESSION['curr_user'];
echo "Please select the courses you have taken so far."."<br>"."<br>";
include 'php_mysql_connect.php'; //retrieving all course from DB
$result = mysqli_query($connect, "SELECT subject, course_number, course_title FROM course ORDER BY course_number");
error_reporting(0);
$num_courses = 83;
while ($row = mysqli_fetch_array($result)) { //HERE's MY PROBLEM AREA
echo
"<input type='checkbox' name='checkbox' value='$row[0] $row[1] $row[2]'>$row[0] $row[1] - $row[2]"."<br>";
//creating checkboxes
}
}
?>
<form action='' method='POST'>
<br><br><input type="submit" name="submit_button" value="Save Courses"><br><br>
</form>
<?php
if(isset($_POST['submit_button'])){ //submitting checked list
echo "Your courses have been saved. Please press NEXT to proceed."."<br>"."<br>";
//I WANT TO CHECK FOR CHECKED BOXES AND INSERT HERE INTO THE DB
}
?>
$nameand$num_recs? The checkboxes aren't in a<form>.