irtrsh Posted February 25, 2009 Share Posted February 25, 2009 Hi I am new to this forum and also new in PHP/MySQL world. I just stuck in taking the check boxes value to MySQL table. I have got a form, there is 3 check box to get the input from the user and stored them to the database. but when I select the values (check box) from the form, it takes all the values and put it in the same column of the table instead of putting them separately. suppose I've the the check box called police, firstname and lastname and when I select all of them it should put the values in the table in police, firstName & lastname column respectively. here is the code.... <html> <head> </head> <body> <form action="rezach1.php" method="post"> <p> please vote for that...<br /> <input type="checkbox" name="form[]" value=999 />police<br /> <input type="checkbox" name="form[]" value="Name" />firstname<br /> <input type="checkbox" name="form[]" value="family" />lastname<br /> </p> <input type="submit" name="formSubmit" value="Submit" /> </form> </body> </html> and this is my php code <?php # DATABASE configuration ######################################### $hostname = "foot.doc.ac.uk"; //server $db_user = "rafor"; // change to your database password $db_password = "gher"; // change to your database password $database = "rafor"; // provide your database name $db_table = "rez"; // leave this as is # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE ###################################### $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($database,$db); ?> <html> <head> <title> Insert Data Into MySQL </title> </head> <body> <?php if($_POST['formSubmit'] != "") { $val = $_POST['form']; if(is_null($val)) { echo("<p>You didn't select any thing.</p>\n"); } else { // echo("<p>You selected door(s): "); for($i=0; $i < count($val); $i++) { echo '<br />'; echo($val[$i] . " "); $val_array = $_POST['form']; foreach ($val_array as $form) { $source .= $form.", "; } $val = substr($source, 0, -2); for ($j=0;$j<count($val_array);$j++) { $num=$val_array[$j]; echo '<br />'; print ("$num "); $sql = "INSERT INTO $db_table(ID, Name, Status) values ('".mysql_real_escape_string(stripslashes($val))."','".mysql_real_escape_string(stripslashes($val))."','".mysql_real_escape_string(stripslashes($val))."')"; } if($result = mysql_query($sql ,$db)) { echo '<h1>Thank you</h1>Your information has been entered into our database<br><br>'; } else { echo "ERROR: ".mysql_error(); } } echo("</p>"); } exit(); } ?> can anyone please help me to sort this out and tell me where is the problem? thank you irtrsh Quote Link to comment https://forums.phpfreaks.com/topic/146872-check-box-help/ Share on other sites More sharing options...
HuggieBear Posted February 25, 2009 Share Posted February 25, 2009 What are the data types of the columns? You got ENUM going on there? Regards Rich Quote Link to comment https://forums.phpfreaks.com/topic/146872-check-box-help/#findComment-771134 Share on other sites More sharing options...
irtrsh Posted February 25, 2009 Author Share Posted February 25, 2009 data type for the 1st colum is Integer, 2nd & 3rd one is VARCHAR.... Quote Link to comment https://forums.phpfreaks.com/topic/146872-check-box-help/#findComment-771181 Share on other sites More sharing options...
napurist Posted February 25, 2009 Share Posted February 25, 2009 I would suggest that you change the checkbox names to 'police', 'firstname', and 'lastname' respectively. Then assign them to variables for processing on your php page. Currently, you have them named "form[]" which is literally "form[]" in hmtl. Its not an array. Quote Link to comment https://forums.phpfreaks.com/topic/146872-check-box-help/#findComment-771298 Share on other sites More sharing options...
HuggieBear Posted February 25, 2009 Share Posted February 25, 2009 Currently, you have them named "form[]" which is literally "form[]" in hmtl. Its not an array. It is in PHP. Regards Rich Quote Link to comment https://forums.phpfreaks.com/topic/146872-check-box-help/#findComment-771453 Share on other sites More sharing options...
napurist Posted February 26, 2009 Share Posted February 26, 2009 It's not in PHP in the html code in the first code box. That is strictly HTML. Unless they just meant to leave out the opening and closing php tags??? Quote Link to comment https://forums.phpfreaks.com/topic/146872-check-box-help/#findComment-771573 Share on other sites More sharing options...
fenway Posted February 28, 2009 Share Posted February 28, 2009 Where's the mysql part of the problem? Quote Link to comment https://forums.phpfreaks.com/topic/146872-check-box-help/#findComment-773297 Share on other sites More sharing options...
HuggieBear Posted March 1, 2009 Share Posted March 1, 2009 It's not in PHP in the html code in the first code box. That is strictly HTML. Unless they just meant to leave out the opening and closing php tags??? You're wrong, when each of those HTML form fields called form[] are passed into PHP, it will see them as items in an array. Create the following file form_test.php <?php echo '<pre>'; print_r($_GET['form']); echo '</pre>'; ?> Now call it with a string like this: form_test.php?form[]=string1&form[]=string2&form[]=string3 This is exactly what the string would like like if you were using the GET method in an HTML form. Something tells me you'll be surprised by the output. Regards Rich Quote Link to comment https://forums.phpfreaks.com/topic/146872-check-box-help/#findComment-773773 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.