0

I have an absolute beginner question here. I'm having trouble getting multiple checkbox entries into a single column in my database. I've tried a few different things with my php but can't seem to get anything to work properly. I've tried using serialize as well as looked into using implode/explode. Sorry I'm basically asking to be spoon-fed because I know its a simple fix, I just haven't been able to get anything to work yet. Any assistance would be appreciated. THANKS!!! Also The three checkboxes I have are system, comm, and other and I do have the brackets for my 3 inputs even though the php isn't setup to handle them yet, so for now my DB is still showing Array for these inputs. Here is my php

    <?php
$con = mysql_connect("localhost","Andrew","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mydb", $con);

$sql="INSERT INTO persons (firstname, lastname, modelid, system, department, comm,  other, shift, comments)
VALUES
  ('$_POST[firstname]','$_POST[lastname]','$_POST[modelid]','$_POST[system]','$_POST[departme    nt]','$_POST[comm]','$_POST[other]','$_POST[shift]','$_POST[comments]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?> 
3
  • Store the checkbox value as comma seperated value in single column. Commented Oct 5, 2012 at 7:01
  • I'm not entirely sure what you mean, sorry, I'm still fairly new at this and far from a coder ;) Thank you for your quick response!! I'm currently researching your suggested solution. Commented Oct 5, 2012 at 7:08
  • I agree with @LearneR, this way you can afterwards split the values by comma and have all your single values again. Commented Oct 5, 2012 at 7:14

5 Answers 5

0

To detail what the comments above said:

$to_insert_in_db = $_POST['system'].','.$_POST['comm'].','.$_POST['other'];

You insert this in a single column in your db, and when you retrieve it you just do:

$retrieved_values = explode(',', $column_value_from_db);

However, if the values of system, comm and so on are a bit more complex(such as strings) i suggest you use json_encode to add the values to the database and json_decode to get them from the database.

$values = array('system' => $_POST['system'], 'comm' => $_POST['comm'], 'other' => $_POST['other']);
$column_to_insert = json_encode($values);
$values = json_decode($column_value_from_the_database);
Sign up to request clarification or add additional context in comments.

2 Comments

if the values of system, comm and so on are a bit more complex(such as strings) - they are checkboxes and so by definition are boolean.
I hate to ask because it just sounds extremely lazy, but would anyone be willing to modify my php and not offer up just a snippet, while I appreciate the effort, I'm not entirely sure how to add any of this code to my existing php. I've been at this for a few days now and can't seem to get anything to work properly. I have no problem admitting my lack of knowledge, but I will continue trying until I succeed. Thanks again for your assistance.
0

Checkbox is having a unique problem i.e. when it is checked there is some value form send with the data, but when it is unchecked there is no variable and it will return undefined variable, so every time you use each checkbox variable do it like this

$check1Val = 0;
if(isset($_POST["check1"]){
  $check1Val = 1;
  //or $check1Val = $_POST["check1"];
}

In last you concate each variable and send it to single coloum

$allCheckValues = $check1Val.",".$check2Val;

Let me know if you still have any confusion about checkboxes or other

Comments

0

Try this

$value ="";
if(isset($checkbox1)){
$value = $value."1";
$flag =1;
}
if(isset($checkbox2)&&$flag==1){
$value = $value.",1";
$flag =1;
}
else{
$value = $value."0,1";
$flag =1;
}  
 if(isset($checkbox3)&&$flag==1){
$value = $value.",1";
$flag =1;
}
else{
$value = $value."0,0,1";
$flag =1;
}  

You can use any value for 0 or 1 depending on your application

1 Comment

Thank you all for your support and quick responses!!! You are awesome!!! I'm definitely still a newbie and I learn best by example, my problem isn't so much understanding snippets of code and what they do, but rather proper code structure. As for your suggestions they are most appreciated however, I'm not sure exactly where to place these snippets and what to remove thereafter, I just haven't run across any examples that are as simple as what I'm looking for. So for now I will continue trying all these suggestions and let you know when when it works. Thanks!!!
0

Storing the checkboxes as a comma separated list in a single field

This is a clean way of doing it that won't produce undefined index/variable notices (keep in mind if a checkbox is unchecked then it won't be sent as a post value), and allows you to add more checkboxes just by adding to the $names array.

<?php

$names = array('system', 'comm', 'other');

$checkboxes = array();

foreach($names as $name)
{
    $value = 0;

    if(isset($_POST['checkboxes']) && is_array($_POST['checkboxes']) && in_array($name, $_POST['checkboxes']))
    {
        $value = 1;
    }

    $checkboxes[] = $value;
}

$checkboxString = implode(',', $checkboxes);

?>

Now you can use $checkboxString in your query. $checkboxString will be a comma separated list of 0s or 1s to indicate the checkbox values. For example: system=off, comm=on, other=on: 0,1,1.

Your HTML needs to look like (using a checkbox array):

<input type="checkbox" name="checkboxes[]" value="system" /> System<br />
<input type="checkbox" name="checkboxes[]" value="comm" /> comm<br />
<input type="checkbox" name="checkboxes[]" value="other" /> other
                                       ^^ square brackets for an array

SQL Injection

You need to research this because your code is vulnerable. Run the user supplied values through mysql_real_escape_string():

$firstname = mysql_real_escape_string($_POST['firstname']);

Query:

VALUES
  ('$firstname', ..............

MySQL Library

Your code is using the old mysql_* library. This is being phased out so you should choose another library for all new code, such as PDO or MySQLi.

Comments

0

So here is what I have used to get my multiple entries into a single cell in my db

('$_POST[firstname]','$_POST[lastname]','$_POST[modelid]','". implode(",", $_POST['system']) ."','$_POST[department]','". implode(",", $_POST['comm']) ."','". implode(",", $_POST['other']) ."','$_POST[shift]','$_POST[comments]')";

So maybe this will serve someone in the future who is trying to accomplish this same simple goal, now my only problem is that the entries in the db are being cut off, I believe the solution is to serialize my arrays. THANKS AGAIN for all of your assistance!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.