how to store and retrieve checkbox value in php?

how to store and retrieve checkbox values in mysql database with php

I was working on a project and i had landed in an awkward situation, where i have to save the tick box values in a database and once done, i had to retrieve those values and present it to the user for editing. i searched everywhere on the web on how to store and retrieve check box values and the answers are hard to find. So i have decided to document her on how it worked for me.

For example when there are checkboxes for 5 colors – Red, Green, Blue, Yellow, Brown and i have the php script doing insertion into database.

Storing checkbox values into a database

The logic is simple. when the user checks those color checkboxes, get the POST value array colors[] then using implode function, i store them in database (single field) separated using comma values.

<form name="form1" method="post" action="">
    <input name="colors&#91;&#93;" type="checkbox" id="colors&#91;&#93;" value="Red">
    <input name="colors&#91;&#93;" type="checkbox" id="colors&#91;&#93;" value="Blue">
    <input name="colors&#91;&#93;" type="checkbox" id="colors&#91;&#93;" value="Green">
    <input name="colors&#91;&#93;" type="checkbox" id="colors&#91;&#93;" value="Yellow">
    <input name="colors&#91;&#93;" type="checkbox" id="colors&#91;&#93;" value="Brown">
    Brown </p>

and the php code to join the selected values is…

//join the post values into comma separated 
$colors = mysql_real_escape_string(implode(',', $_POST['colors']));

and then store this value in the database in a single TEXT field in mysql.

Retrieving from Database and show checkboxes

For example if user selects Red, Blue, Brown checkboxes and we have those values stored in database. Now the user wants to edit those checkbox options in a form and now we have to show all check box colors with Red, Blue and Brown checked.

This is kind of tricky but there is a way to do it.

The logic goes this way..

1. Make an array variable containing all colors.
2. Retrieve those colors stored in database and make them another array using explode function separated by commas.
3. Loop through all colors and check whether each color is stored in database. If so write a SELECTED checkbox else write a unselected checkbox. To accomplish the array match, i will be using in_array() function in php.

The php code is below…

// $row&#91;'color&#93; is a database field.

$aColors = array("Red", "Blue", "Green", "Yellow", "Brown");

//converting comma separated into array using explode function
	$dbcolors= explode(',',$row&#91;'color'&#93;);
  foreach ($aColors as $color) {
	 if(in_array($color,$dbcolors)) {
	  echo "<input name=\"colors&#91;&#93;\" type=\"checkbox\" value=\"$color\" CHECKED> $facil ";
		  } else
	  echo "<input name=\"colors&#91;&#93;\" type=\"checkbox\" value=\"$color\"> $color";

NOTE: The above code may not work since double quotes with echo must be escaped.

Upon running this code, you will see all check boxes with colors with values stored in database selected.

Hope this helps!

Similar Posts:


Balakrishnan Prabhu

Mr. Balakrishnan Prabhu is the founder of Corpocrat magazine. He is also the founder of Best Citizenships (BC), assisting wealthy individuals with with global citizenship and residency programs in Europe. His other interests are Linux, Machine learning, Wordpress, etc. You can contact him here

  • Thanks a lot , it was very useful for me

  • thanks in advanced to answer my question?

    what should i do if the checkbox will be send without any tick and parameter;
    when the array is empety
    how do i keep a way from this message : Undefined index

  • thanks in advanced to answer my question?

    what should i do if the checkbox will be send without any tick and parameter;
    when the array is empty
    how do i keep a way from this message : Undefined index

  • daraseng

    Thanks you BIG BIG…
    you save my full day!!!

  • Keith

    Why doesn’t this work?

    I include a database file and make two separate files including it and it yields errors with defining the variables.

    Similar questions have been posed regarding this. Is there a solution?

  • Jeff

    Hi, I have a smiliar code like this, retrieving stored data value from mysql but after displaying it on the page, can I use the results shown to update on the same page?

    Hope you guys understand. =x

  • Hugo

    I have a doubt concerning:

    1. Make an array variable containing all colors.

    I have a table in my database with all the possible color values, how do I get to something like this:

    $aColors = array(“Red”, “Blue”, “Green”, “Yellow”, “Brown”);

  • ehsan

    Prabhu Many thanks for sharing this useful tutorial.

    I am a little puzzled by one variable on your php code line number 13
    What does this variable $facil do over here on line 13?

  • v k

    i want to get database table fields with checkbox and if i select any check box either 1 or more files trough check boxes it will get the data what i have in the checked fields in database … using php … i’m using xampp..
    can you help me plz

    thank you