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?

Thanks!

Dani AI

Generated

A common root cause for the literal string "Array" showing up in the database is that an array variable was used where a string was expected. In this thread eventually found the real problem: the form was posting to the wrong script, so the code that should have converted the checkbox array to a storable string never ran. That explains the symptom without any exotic bug in PHP itself.

Quick checklist that solves most similar cases:

  • Confirm the form's method and action point to the correct file, and that the checkboxes use a name ending in [] so PHP receives an array.
  • Inspect what was actually posted (for debug use var_export($_POST, true) or print_r($_POST) before any database work).
  • Ensure the code converts the array to a storable format (and does not pass the raw array into the query).
  • Use a TEXT/JSON column (or a normalized join table) rather than relying on ad-hoc CSV strings.
  • Avoid old mysql_* APIs; use PDO or mysqli with prepared statements to prevent SQL issues and make debugging easier.

Example (safe modern approach: JSON + PDO)

$days = !empty($_POST['days']) ? array_values($_POST['days']) : [];
$days_json = json_encode($days);

$pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8mb4', 'user', 'pass', [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$stmt = $pdo->prepare('INSERT INTO users (days) VALUES (:days)');
$stmt->execute([':days' => $days_json]);

Retrieval:

$stmt = $pdo->query('SELECT days FROM users WHERE verified = 0');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $days = json_decode($row['days'], true) ?: [];
  foreach ($days as $d) {
    echo htmlspecialchars($d, ENT_QUOTES) . "<br>\n";
  }
}

For queryability and long-term maintainability prefer a normalized table (e.g. user_days with user_id + day) or MySQL JSON columns where supported. The counting loop suggested by is usually unnecessary; count() or foreach will handle sizes correctly. And for , a "table does not exist" error means checking the selected database and confirming the users table was created.

Recommended Answers

All 6 Replies

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

$a=0;
foreach ($days as $value){
$a=$a+1;
}
for($i=0;$i<$a;$i++) 
{
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());

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.