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

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

Postby kwh01 » Wed Apr 11, 2012 8:05 am

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.
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

Postby social_experiment » Wed Apr 11, 2012 8:15 am

The syntax for the query seems ok; the column you are inserting into (i'm guessing it will be subjectnumber), is it marked 'unique'?
First, solve the problem. Then, write the code.
“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
When asking for assistance give as much information about the problem as possible
User avatar
social_experiment
DevNet Master
 
Posts: 2688
Joined: Sun Feb 15, 2009 12:08 pm
Location: South Africa

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

Postby kwh01 » Wed Apr 11, 2012 8:18 am

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
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

Postby social_experiment » Wed Apr 11, 2012 8:47 am

Amend the code to the example below and paste the data printed to the browser
Syntax: [ Download ] [ Hide ]
<?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
                                        }
                                        */

                                }
?>
First, solve the problem. Then, write the code.
“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
When asking for assistance give as much information about the problem as possible
User avatar
social_experiment
DevNet Master
 
Posts: 2688
Joined: Sun Feb 15, 2009 12:08 pm
Location: South Africa

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

Postby kwh01 » Wed Apr 11, 2012 8:47 am

Syntax: [ Download ] [ Hide ]
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

Postby kwh01 » Wed Apr 11, 2012 8:51 am

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.
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

Postby social_experiment » Wed Apr 11, 2012 9:08 am

what did you do to solve the issue
First, solve the problem. Then, write the code.
“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
When asking for assistance give as much information about the problem as possible
User avatar
social_experiment
DevNet Master
 
Posts: 2688
Joined: Sun Feb 15, 2009 12:08 pm
Location: South Africa

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

Postby kwh01 » Wed Apr 11, 2012 11:45 pm

social_experiment wrote:what did you do to solve the issue

Syntax: [ Download ] [ Hide ]
if (!mysql_query($qry))
 {
    die('Error: ' . mysql_error());
}
 



Hi social_experiment, i just removed this line and it work perfectly.
kwh01
Forum Newbie
 
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Previous

Return to PHP - Code

Who is online

Users browsing this forum: Google [Bot] and 8 guests