Problem with saving the subjects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ShadowSkill
Forum Newbie
Posts: 22
Joined: Sun Jan 01, 2012 5:45 am

Problem with saving the subjects

Post 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 ???
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem with saving the subjects

Post 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);
(#10850)
Post Reply