I am trying to store the contents of checkboxes in a field in a mysql database and retireve them later...

<input type="checkbox" name="days[]" class="other" value="Monday" /> 
   <label class="left">Monday</label> <br /> 
   <input type="checkbox" name="days[]" class="other" value="Tuesday" /> 
   <label class="left">Tuesday</label> <br /> 
   <input type="checkbox" name="days[]" class="other" value="Wednesday" /> 
   <label class="left">Wednesday</label> <br /> 
   <input type="checkbox" name="days[]" class="other" value="Thursday" /> 
   <label class="left">Thursday</label> <br /> 
   <input type="checkbox" name="days[]" class="other" value="Friday" /> 
   <label class="left">Friday</label> <br /> 
   <input type="checkbox" name="days[]" class="other" value="Saturday" /> 
   <label class="left">Saturday</label> <br /> 
   <input type="checkbox" name="days[]" class="other" value="Sunday" /> 
   <label class="left">Sunday</label> <br />
$days = implode(",",$_POST['days']); 
mysql_query("INSERT INTO users (..., days, ...) VALUES (.., '$days', ..)") or die("something went wrong during the registration. MySQL said: ".mysql_error());

(the "..." means that there is other information, yes those are confirmed working, inserted as well)

This is what I use to retrieve the data:

$result =  mysql_query("SELECT * FROM users where verified = 0");
while($info = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$days = explode(",", $info['days']);
	foreach ($days as $value){
		echo $value;
		echo "<br/>";

(obviously, the .'s mean other - confirmed working - code)

Problem is that the $value comes out as "array"...what the heck am I doing wrong?


10 Years
Discussion Span
Last Post by jckb188

try to have a counter so you can display your data:

foreach ($days as $value){
echo $days[$a];
echo "<br/>";

Ok. I just tried that..same thing...

when I echo a$ (after the first foreach), it gives a value of 1

so then I echo $days[0] (the first record)
its gives a value of 'array'

Got frustrated, so I even tried to populate $days like this(starting with an empty array and adding each value the array one by one, then implode to a single comma-delimited string...seems logical) -- ended up with same dang result though...grrrr

$days_array = array("");
foreach ($_POST['days'] as $val2){
	$days_array[] = $val2; 
$days = implode(",",$days_array);

I guess my next step is to take them out of the checkbox array, give each box a seperate name and add them to an array manually in the code.

I think its just a matter of missing something simple...guess I am just overlooking it.... :(


Here's what confusing me...

when I store the imploded array, it SHOULD be a comma-delimated string of the values...BUT, it appears to have the value 'ARRAY', which tells me that the implode didn't work, i.e. the $_POST array stored not as a string of values seperated by commas, but as an array(hence the value ARRAY showing up in the $data field).

Am I missing a step somewhere when storing the field??


heres a stupid mistake...LOL

When I created the form with the checkboxes, the post was to a file called register.php...I changed it to the name of this file register2.php, but amoungst my edits, I inadvertently reset it back to register.php, hence it was posting from the old file not the new one and thus giving me the error(s).

Gawd...I had the code right many many hours ago but one stupid CTRL-Z too many in Dreamweaver and it messed it all up arrrgh haha

Thanks guys :)


There are many established systems for locating information (e.g. documents, images, emails, patents, internet content or media content such as audio/video content) by searching under keywords. Examples include internet search "engines" such as those provided by "Google" TM or "Yahoo" TM where a search carried out by keyword leads to a list of results which are ranked by the search engine in order of perceived relevance.


Had the exact same code, only I get an error "users does not exist"...

I thought the act of inserting would initialize it? I am a noobie if anybody can help thanks in advance!

mysql_query("INSERT INTO users (..., days, ...) VALUES (.., '$days', ..)") or die("something went wrong during the registration. MySQL said: ".mysql_error());

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.