Page 1 of 1

Problem with saving the subjects

Posted: Sat Feb 04, 2012 12:18 am
by ShadowSkill
Example code:

Code: Select all

<input type="checkbox" name="subject[]" value="ENG" />English
<input type="checkbox" name="subject[]" value="MATH" />Mathematics
<input type="checkbox" name="subject[]" value="SCI" />Science
<input type="checkbox" name="subject[]" value="FIL" />Filipino


<?php
//and the post data would look like

$arrSubjects = $_POST['subject'];//retruns array. Ung mga nakachecked na value na agad ung laman nito

for($i=0;$i<count($arrSubjects);$i++){
   mysql_query("INSERT INTO table_name (sub) values ('".$arrSubjects[$i]."')");
}

?>
Courtesy of Sir samypogs who help me with the checkbox problem...

I have a problem saving that to my database if i checked it...
The Output when i look at my phpmyadmin is like this

Code: Select all


| sub | 
 MATH
 ENG
 SCI
 FIL
My problem is if i enter another student and subjects it would be saved again at the same table which i do not want since i will later show the the record of the student with the according subjects...
Please help me clear myself out i'm confused ???

Re: Problem with saving the subjects

Posted: Sat Feb 04, 2012 1:52 am
by Christopher
Try something like this:

Code: Select all

   $values = array();
    foreach ($arrSubjects as $sub) {
        $values[] = "('" . mysql_real_escape_string($sub) . "')";
    }
    $sql = "INSERT INTO table_name (sub) VALUES " . implode(',', $values);
    mysql_query($sql);