Jump to content
Larry Ullman's Book Forums
herdhemhola

How to insert multiple select arrays in php/mysql

Recommended Posts

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>

I have about 8 subjects and grades to be inserted like that

 

Also this is my insert.php

 

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"; 
            }
}

 

Nothing was inserted into the subjects, grades, and results.

Please help me as I am new to php/mysqli

Share this post


Link to post
Share on other sites

Okay, so this kind of thing is a bit tricky. Since you have multiple inputs that are all arrays of related records, I'd start by changing your naming scheme to make them all more overtly related. For example, you'd want exams[X][subject] and exams[X][grade]. If i'm following you on this. I'm not sure I am 100%. But the key is to make the association of the data overt like that. Let me know if it's still unclear.

Share this post


Link to post
Share on other sites
On 10/6/2019 at 2:24 AM, Larry said:

Okay, so this kind of thing is a bit tricky. Since you have multiple inputs that are all arrays of related records, I'd start by changing your naming scheme to make them all more overtly related. For example, you'd want exams[X][subject] and exams[X][grade]. If i'm following you on this. I'm not sure I am 100%. But the key is to make the association of the data overt like that. Let me know if it's still unclear.

It's still unclear....did you mean I should change my html to this results[][subjects] and results[][grades]. What am just trying to do is to insert results in form of (English C5, Maths A1, Physics C5 etc) all in one row and under the column results.

Thanks sir

Share this post


Link to post
Share on other sites

Yes, more or less. You'll want to change your naming scheme so that each row of input--which becomes a database row--ends up as part of the same array in PHP. You'll need to play around with what works, but if you do var_dump($_POST) in the PHP code that may help you understand what's coming in from the form. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...