kevinkhan Posted January 25, 2010 Share Posted January 25, 2010 I have this form <?php echo "<input class=\"events\" type=\"checkbox\" name=\"events[]\" value=\"Teenage Discos\" /><span>Teenage Discos</span><br />";?> <?php echo "<input class=\"events\" type=\"checkbox\" name=\"events[]\" value=\"Teenage Discos\" /><span>Teenage Discos</span><br />";?> <?php echo "<input class=\"events\" type=\"checkbox\" name=\"events[]\" value=\"Teenage Discos\" /><span>Teenage Discos</span><br />";?> <?php echo "<input class=\"events\" type=\"checkbox\" name=\"events[]\" value=\"Teenage Discos\" /><span>Teenage Discos</span><br />";?> <?php echo "<input class=\"events\" type=\"checkbox\" name=\"events[]\" value=\"Teenage Discos\" /><span>Teenage Discos</span><br />";?> and trying to extract the values out and insert them into a database.. $events = $_POST['events']; $query = "INSERT INTO user ( events ) VALUES ( '{$events}' )"; $result = mysql_query($query, $connection); When i go to the database it shows up as array.. How do i get it to show up as the values? Thanks Link to comment https://forums.phpfreaks.com/topic/189727-php-checkbox-problem/ Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2010 Share Posted January 25, 2010 $_POST['events'] is an array because you have multiple checkboxes. So you'll need to loop through each of the checkboxes when doing your insert: for($i=0; $i<count($_POST['events']); $i++) { mysql_query("INSERT INTO user (events) VALUES ('" . $_POST['events'][$i] . "')", $connection); } Link to comment https://forums.phpfreaks.com/topic/189727-php-checkbox-problem/#findComment-1001271 Share on other sites More sharing options...
kevinkhan Posted January 25, 2010 Author Share Posted January 25, 2010 my query is actually like this $firstName = mysql_prep($_POST['firstName']); $lastName = trim(mysql_prep($_POST['lastName'])); $mobile = mysql_prep($_POST['mobile']); $email = mysql_prep($_POST['email']); $county = trim(mysql_prep($_POST['county'])); $dateOfBirth = mysql_prep($_POST['date3']."-".$_POST['date2']."-".$_POST['date1']); $gender = trim(mysql_prep($_POST['gender'])); $events = $_POST['events']; $comments = trim(mysql_prep($_POST['comments'])); $query = "INSERT INTO user ( firstName,lastName,mobile,email,county,dateOfBirth,gender,events,comments ) VALUES ( '{$firstName}','{$lastName}','{$mobile}','{$email}','{$county}','{$dateOfBirth}','{$gender}','{$events}','{$comments}' )"; is there anyway of linking your code into this? Link to comment https://forums.phpfreaks.com/topic/189727-php-checkbox-problem/#findComment-1001274 Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 if the events column of your database just takes a comma separated string of the values of the checked checkboxes then where you have $events = $_POST['events']; you could have $events = implode(",",$_POST['events']); Link to comment https://forums.phpfreaks.com/topic/189727-php-checkbox-problem/#findComment-1001297 Share on other sites More sharing options...
kevinkhan Posted January 25, 2010 Author Share Posted January 25, 2010 Thanks thats exactly what i was looking for Link to comment https://forums.phpfreaks.com/topic/189727-php-checkbox-problem/#findComment-1001383 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.