I had the following tables in my dabatase: tbl_students and tbl_courses.
I have in mind a project for a music school where a student can sign for 1 or more courses, for example, when I register a student more than one course can be chosen, like guitar and piano, deppending on his choice (for now i'm registering students by myself so this is just hypothetical).
I found that i had to deal with many to many relationships as 1 student can have many courses and in a course there are many students inscribed in them.
The next step I did was creating an intermediate table that i called tbl_students_courses with the primary keys of both tables.
My tables look like this:
tbl_students
+---------+---------+---------------+---------+
| st_id | st_name | st_email | st_code |
+---------+---------+---------------+---------+
| 1 |John Doe | doe@gmail.com | 55555 |
+---------+---------+---------------+---------+
tbl_courses
+---------+-----------+--------------------------+
| cr_id | cr_name | cr_desc |
+---------+-----------+--------------------------+
| 1 | Guitar | Guitar course description|
+---------+-----------+--------------------------+
| 2 | Bass | Bass course description |
+---------+-----------+--------------------------+
| 3 | Violin | Violin course description|
+---------+-----------+--------------------------+
| 4 | Drums | Drums course description |
+---------+-----------+--------------------------+
| 5 | Piano | Piano course description |
+---------+-----------+--------------------------+
tbl_students_courses
+---------+---------+---------+
| st_id | cr_id |date_insc|
+---------+---------+---------+
| ----- | ----- | ----- |
+---------+---------+---------+
Now my problems are, I don't know how can i associate the tables with my php code. I assume that if i choose 2 courses when i sign up the student it should create 2 records on the intermediate table where only the cr_id differs from each other.
The other problem is getting the ID of the courses through the checkboxes with php, so when i select, for example, the piano and guitar checkboxes, it should return the id of both courses, run a query and insert the records.
Note: With my php code, the foreach loop is obviously just picking up the strings and not the ID of the course i wanted.
Here is my HTML:
<div class="form-group">
<label class="col-form-label">Select the courses</label>
<div class="form-check">
<div class="form-check form-check-inline">
<input class="form-check-input" name="course[]" type="checkbox" value="Guitar">
<label class="form-check-label" >Guitar</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" name="course[]" type="checkbox" value="Bass">
<label class="form-check-label" >Bass</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" name="course[]" type="checkbox" value="Violin">
<label class="form-check-label" >Violin</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" name="course[]" type="checkbox" value="Drums">
<label class="form-check-label" >Drums</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" name="course[]" type="checkbox" value="Piano">
<label class="form-check-label" >Piano</label>
</div>
</div>
Here is my PHP:
<?php
if (isset($_POST['submit'])) {
include_once '../includes/functions.php';
$studentName = addslashes($_POST['sname']);
$studentEmail = addslashes($_POST['semail']);
$studentCode = intval($_POST['scode']);
$studentcourse = $_POST['course'];
$query = "INSERT INTO tbl_students (st_name, st_id, st_code) "
. "VALUES ('$studentName', '$studentEmail', '$studentCode')";
$link = connection_db();
$result = mysqli_query($link, $query);
if($result){
$sql = "SELECT LAST_INSERT_ID()";
$result = mysqli_query($link, $sql);
$last = mysqli_fetch_assoc($result);
foreach($studentcourse as $value){
$data = date('Y-m-d H:i:s');
$lastID =($last["LAST_INSERT_ID()"]);
$sql = "INSERT INTO tbl_students_courses (st_id, cr_id, date_insc) VALUES ($lastID, '$value', '$data')";
$result = mysqli_query($link, $sql);
}
echo "<script>alert('Data saved successfully');</script>";
print "<script>top.location = 'index.php?id=2';</script>";
} else {
echo "<script>alert('ERROR! Could not save data');</script>";
}
}
?>
Any help or suggestions to improve the question are highly appreciated.