How to get checkbox value and compare with table row value

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

kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: How to get checkbox value and compare with table row val

Post by kwh01 »

social_experiment wrote:
kwh01 wrote:it inserted 2 rows at once. hmm. cannot figured it out why
How many subjects did you select?
perviously is two, but now i get some other error again = =


here is the query i trying to insert the subject number and student ID inserted

but now, no more inserted 2 row per once. it can insert into my table but show me this error.
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

and im trying to select 2 checkbox but it only insert the 1st record into table only
Last edited by kwh01 on Sat Apr 14, 2012 3:58 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get checkbox value and compare with table row val

Post by social_experiment »

The syntax for the query seems ok; the column you are inserting into (i'm guessing it will be subjectnumber), is it marked 'unique'?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: How to get checkbox value and compare with table row val

Post by kwh01 »

social_experiment wrote:The syntax for the query seems ok; the column you are inserting into (i'm guessing it will be subjectnumber), is it marked 'unique'?
hmm, nope. i did not set any key for subjectnumber or studentID in table
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get checkbox value and compare with table row val

Post by social_experiment »

Amend the code to the example below and paste the data printed to the browser

Code: Select all

<?php
foreach($_POST['stusubject']  as $subjectnumber)
                                {
                                        $sql="INSERT INTO $tbl_name(studentID,subjectnumber)VALUES('$_SESSION[stuID]','$subjectnumber')";
                                        echo $sql . '<br />';
                                        /*
                                        $qry = mysql_query($sql);
                                        if (!mysql_query($qry))
                                        {
                                        //die('Error: ' . mysql_error());
                                            echo 
                                        }
                                        */
                                }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: How to get checkbox value and compare with table row val

Post by kwh01 »

Code: Select all

if (!mysql_query($qry))
 {
die('Error: ' . mysql_error());
}
sir, after i removed this line, it works like a charm. omg.
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: How to get checkbox value and compare with table row val

Post by kwh01 »

thank you sir for assisting me . finally i can check the checkbox and insert into my database.
thanks alot
Last edited by kwh01 on Sat Apr 14, 2012 3:58 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get checkbox value and compare with table row val

Post by social_experiment »

what did you do to solve the issue
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: How to get checkbox value and compare with table row val

Post by kwh01 »

social_experiment wrote:what did you do to solve the issue

Code: Select all

if (!mysql_query($qry))
 {
    die('Error: ' . mysql_error());
}

Hi social_experiment, i just removed this line and it work perfectly.
Post Reply