Jump to content

PHP Checkbox problem


kevinkhan

Recommended Posts

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

$_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);
}

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.