Please I need your help, I am new to PHP and I need your help in inserting multiple select array into the database in the form of this.
This is what I want to achieve. My table has 5 columns (id, examno, subjects, grades, results). I want results column to be inform of subjects grades, subjects grades.........depending on the numbers of subjects and grades users select (e.g English C6, Mathematics C6) all in one column results.

This is my html codes
<form action="insert.php">
<div class="form-group">
<label>Exam Number</label>
<input type="text" class="form-control" name="examno" id="examno">
</div>
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="table table-borderless" id="example">
<tr>
<td width="12%"><label class="control-label">S/NO</label></td>
<td width="53%"><label class="control-label">SUBJECTS</label></td>
<td width="35%"><label class="control-label">GRADE</label></td>
</tr>
<tr>
<td>1</td>
<td>
<select name="subjects[]" class="form-control" id="subject">
<option value="" selected="selected">Select subject</option>
<option value="English">English</option> <option value="Mathematics">Mathematics</option>
</select>
</td>
<td>
<select name="grades[]" class="form-control">
<option value=""> Select</option>
<option value="A1">A1</option>
<option value="B2">B2</option>
</select>
</td>
</tr>
</table>
<input type="submit">
</form>

This is my insert.php codes

if(isset($_POST['submit'])){
$examno = mysqli_real_escape_string($conn, $_POST['examno']);
foreach($_POST['subjects'] as $row=>$subjects){
$subjects = mysqli_real_escape_string($conn, $subjects);
$grades = mysqli_real_escape_string($conn, $_POST['grades'][$row]);
$results = $subjects." ".$grades;
}
$sql = "INSERT INTO qualifications(examno, subjects, grades, results) VALUES('".$examno."', '".$subjects."', '".$grades."', '".$results."')";
$result = mysqli_query($conn, $sql);
if($result){
header("location:declaration.php"); // user will be taken to the success page
}
else{
echo "Oops. Something went wrong. Please try again";
}
}

I have about 8 subjects to be stored into the database.
Please help me

    This is what I want to achieve. My table has 5 columns (id, examno, subjects, grades, results). I want results column to be inform of subjects grades, subjects grades.........depending on the numbers of subjects and grades users select (e.g English C6, Mathematics C6) all in one column results.

    Are you sure you want to do that? It sounds like you have a specific output in mind, and used this to design your database table. While there might be good reasons for doing so, this is usually a wrong approach. You can construct the "results" output as needed.

    Furthermore, it might make more sense to have a second table, e.g., grades, that records the grade for each subject, alongside the qualification that the grade is for. You could even go further to have a subjects table with all the unique subjects.

      This isn't about your problem (I had something to comment but rereading I realised laserlight had understood better once I saw how what the results column was supposed to contain), but I'll add a couple of notes that will be applicable even if you take his suggestions.

      Your code needs to check that a subject(s) and grade(s) have actually been selected, because at the moment they are both capable of being empty strings if someone, say, accidentally hits submit a little too quickly. (And if they were being malicious/mischievous there's nothing stopping them from submitting a Z9001 grade in 💃).

      Weedpacket
      if they were being malicious/mischievous there's nothing stopping them from submitting a Z9001 grade ...

      Haha ... I found a bug in our cart the other day. We allow you to purchase credit against future purchases. You could input a negative credit and get your cart checked out for free. 😅

      Fortunately, no evidence of this ever being done. 😂 Apparently I need to work on my abs() on more ways than just at the gym 😃 😃

        Write a Reply...