0

Hi I have a section where I am cloning a div for "Add another" button everytime it's clicked, the form fields are set as arrays. The query runs but saves nothing and just displays empty rows... can someone please tell me where could I be wrong?

<label for="GCSESubject[]">GCSE Subjects</label>
<select name="GCSESubject[]" id="GCSESubject[]" style="width: 178px; float: left; margin-right: 12px;"> 
    <option value=""></option> 
    <?php echo getGCSESubjectsOptions(false, true);?> 
</select> 

Then I save it using these lines of code:

function saveGCSEEducation() 
{ 
    if (isset($_POST['saveGCSEEducation'])) { 

        $db = new Connection(DB_HOST, DB_USER, DB_PASS, DB_NAME); 

        $GCSESubjectSerialized = serialize($_POST['GCSESubject']); 
        $GCSESubject=mysql_real_escape_string($GCSESubjectSerialized); 
        $userID = $_SESSION['user']['userID']; 

        $db->query('
            INSERT INTO GCSEEducation 
            (userID, GCSESubject, GCSEGrade) 
            VALUES 
            ("'.$userID.'", "'.$GCSESubject.'", "'.$GCSEGrade.'")  
        '); 
    }
}
0

1 Answer 1

1

You must loop an array before entering its values to database as follows:

function saveGCSEEducation() 

{ 
    if (isset($_POST['saveGCSEEducation'])) { 

        $db = new Connection(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
        $count= count($_POST['GCSESubject']);
        for($i=0;$i<$count;$i++)
        {
        $GCSESubjectSerialized = serialize($_POST['GCSESubject'][$i]); 
        $GCSEGradeSerialized = serialize($_POST['GCSEGrade'][$i]); 
        $GCSESubject=mysql_real_escape_string($GCSESubjectSerialized); 
        $GCSEGrade=mysql_real_escape_string($GCSEGradeSerialized); 
        $userID = $_SESSION['user']['userID']; 
        $db->query(' 

            INSERT INTO GCSEEducation 

            (userID, GCSESubject, GCSEGrade) 

            VALUES 

            ("'.$userID.'", "'.$GCSESubject.'", "'.$GCSEGrade.'")  

            '); 
           }
    }
}

Hope it helps!!

Sign up to request clarification or add additional context in comments.

5 Comments

Hey thanks a lot, it works but just one prob, it's adding an extra blank record containing 0, 0 values, how can I get rid of that ?
can you please write print_r($_POST['GCSESubject']); so that I can find the problem
On your suggestion @Echo I am doing this as there are two fields. $count= count($_POST['GCSESubject']); for($i=0;$i<$count;$i++) { $GCSESubject = mysql_real_escape_string($_POST['GCSESubject'][$i]); $GCSEGrade = mysql_real_escape_string($_POST['GCSEGrade'][$i]); $db->query(' INSERT INTO GCSEEducation (userID, GCSESubject, GCSEGrade) VALUES ("'.$userID.'", "'.$GCSESubject.'", "'.$GCSEGrade.'") '); }
right print_r($_POST['GCSESubject']); at start of function then tell me the output of it
It was not printing anything but I just applied a !empty check for one of the two and it worked, yayyy, thanks a lot Echo, You Rock!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.