joelphil Posted June 24, 2006 Share Posted June 24, 2006 hello all, my goal has been to insert data into mysql using a form with checkboxes:Here's my basic form:<form action="POST" METHOD="insertdata.php"><INPUT type=checkbox name="choice[]" value="one">One<INPUT type=checkbox name="choice[]" value="two">Two<INPUT type=checkbox name="choice[]" value="three">Three<INPUT type=checkbox name="choice[]" value="four">Four<INPUT type=submit name=submit value="Submit">Here is basic table for info"$sql = Insert into tbl_name (choice1,choice2, choice3, choice4) VALUES (...you get the picture. so, I have tried counting array and using foreach but, I am unable to resolve this problem. any help would be great. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/12808-inserting-data-into-mysql-using-a-form-with-checkboxes/ Share on other sites More sharing options...
wildteen88 Posted June 24, 2006 Share Posted June 24, 2006 Something like this:[code]<?phpif(isset($_POST['submit'])){ for($i = 0; $i <= 3; $i++) { $choice[$i] = isset($_POST['choice'][$i]) ? $_POST['choice'][$i] : 'NULL'; } $sql = "INSERT INTO tbl_name (choice1, choice2, choice3, choice4) VALUES ('" . implode("', '", $choice) . "')";# echo "Query built: <code>" . $sql . '</code><br /><br />';}?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="post"><INPUT type=checkbox name="choice[0]" value="one">One<INPUT type=checkbox name="choice[1]" value="two">Two<INPUT type=checkbox name="choice[2]" value="three">Three<INPUT type=checkbox name="choice[3]" value="four">Four<INPUT type=submit name=submit value="Submit">[/code]Probably a long route but it does what you want to do. Quote Link to comment https://forums.phpfreaks.com/topic/12808-inserting-data-into-mysql-using-a-form-with-checkboxes/#findComment-49088 Share on other sites More sharing options...
kenrbnsn Posted June 24, 2006 Share Posted June 24, 2006 Since only those checkboxes that are checked are returned to your script, you should change your form to be something like this:[code]<INPUT type=checkbox name="choice[1]" value="one">One<INPUT type=checkbox name="choice[2]" value="two">Two<INPUT type=checkbox name="choice[3]" value="three">Three<INPUT type=checkbox name="choice[4]" value="four">Four[/code]Then in your script you can do something like:[code]<?phpif (isset($_POST['choice'])) { // only do this if any of the boxes were checked $tmpq = array(); foreach($_POST['choice'] as $key => $value) $tmpq = 'choice' . $key . " = '" . $value . "'"; $sql = 'insert into tablename set ' . implode(', ',$tmpq); $res = mysql_query($sql) or die("There was a problem with the query: $sql<br>" . mysql_error());}?>[/code]Note, that I've used the alternative format for the Mysql INSERT statement.Ken Quote Link to comment https://forums.phpfreaks.com/topic/12808-inserting-data-into-mysql-using-a-form-with-checkboxes/#findComment-49090 Share on other sites More sharing options...
joelphil Posted June 24, 2006 Author Share Posted June 24, 2006 Wildteen, thanks for your help but, it didn't insert data.I appreciate your time and help.-Joel[!--quoteo(post=387472:date=Jun 24 2006, 10:53 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 24 2006, 10:53 AM) [snapback]387472[/snapback][/div][div class=\'quotemain\'][!--quotec--]Something like this:[code]<?phpif(isset($_POST['submit'])){ for($i = 0; $i <= 3; $i++) { $choice[$i] = isset($_POST['choice'][$i]) ? $_POST['choice'][$i] : 'NULL'; } $sql = "INSERT INTO tbl_name (choice1, choice2, choice3, choice4) VALUES ('" . implode("', '", $choice) . "')";# echo "Query built: <code>" . $sql . '</code><br /><br />';}?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="post"><INPUT type=checkbox name="choice[0]" value="one">One<INPUT type=checkbox name="choice[1]" value="two">Two<INPUT type=checkbox name="choice[2]" value="three">Three<INPUT type=checkbox name="choice[3]" value="four">Four<INPUT type=submit name=submit value="Submit">[/code]Probably a long route but it does what you want to do.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/12808-inserting-data-into-mysql-using-a-form-with-checkboxes/#findComment-49101 Share on other sites More sharing options...
wildteen88 Posted June 24, 2006 Share Posted June 24, 2006 Thats because you need to add the code in for that! Which is the following:mysql_query($sql) or die(mysql_error());I thought you had that, thats why I left it out. Quote Link to comment https://forums.phpfreaks.com/topic/12808-inserting-data-into-mysql-using-a-form-with-checkboxes/#findComment-49106 Share on other sites More sharing options...
kenrbnsn Posted June 24, 2006 Share Posted June 24, 2006 Did you look at the sample code I posted? It's almost the same a wildteen's with a few differences with how I construct the query.Ken Quote Link to comment https://forums.phpfreaks.com/topic/12808-inserting-data-into-mysql-using-a-form-with-checkboxes/#findComment-49109 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.