SQL Array - Only outputs "Array"

Hi All

I’m new to PHP so go easy…

I’m stuck on an issue where i have a form with multiple checkboxes, however when i post the values to the database, it submits “Array” instead of the values. The SQL table then shows “Array” along with the page i’m echoing out to.

Also i’m getting a: PHP Notice: Array to string conversion

I’ve looked at foreach loop, implode & explode but i’m struggling to work out how to get where i need to be. After exhausting google, i cant seem to find the right answer. Please can someone tell me what needs adjusting to make this work…

Please note this is in a password protected members only area which is why there’s no escapestring etc. Also i would prefer to keep all options in one table rather than having them on a separate table and doing an SQL join.

Extracts from the filesubmit.php

	if (isset($_POST['free_options']))
	{
	$free_options= $_POST['free_options'];
	foreach ($free_options as $key => $value){
		

	if (isset($_POST['paid_options']))
	{
	$paid_options= $_POST['paid_options'];
	foreach ($paid_options as $key => $value){
		}
	}

$sql = "INSERT INTO XXXXXX (user_id, free_options, paid_options) VALUES (?, ?, ?)";
<div class="col-md-3">
	<span class="badge badge-success">£0</span>
		 <input type="checkbox" value="Option1" name="free_options[]"> Option1<br>
</div><div class="col-md-3">
	<span class="badge badge-success">£0</span>
		 <input type="checkbox" value="Option2" name="free_options[]"> Option2<br>
</div><div class="col-md-3">
	<span class="badge badge-success">£0</span>
		 <input type="checkbox" value="Option3" name="free_options[]"> Option3<br>
</div>

extract from the read page:-

 echo "<th>Free Options</th>";
 echo "<th>Paid Options</th>";
 echo "</tr>";
 echo "</thead>";
 echo "<tbody>";
while($row = mysqli_fetch_assoc($result)){
    echo "<tr>";
    echo "<td>" . $row['free_options'] . "</td>";
    echo "<td>" . $row['paid_options'] . "</td>";

Thank you in advance.

An array’s .toString() method simply returns “Array”.
I have concerns about your database structure, as it appears you’re trying to stuff multiple values into a single field.

With the query as structured, implode the array and use it as a string.

1 Like

Thanks m_hutley. That’s exactly what I’m trying to do :stuck_out_tongue_closed_eyes:.

Please can you confirm how I would format the implode function within the $row to output to the table?

a row returned from a database will contain no arrays, because databases dont store arrays.

What line of your script is throwing the array to string conversion?

1 Like

If that’s why you want to put multiple values into a single column, then I suspect you will come to regret that when you see how much more complex it is to use the information you stored. There may be a very small number of cases where stuffing multiple values into one column won’t cause you any problems, but generally, it is better to either have a column for each value, or a separate linked table.

Just out of interest, why would you prefer to do it that way?

If you really, really want to do it, converting the array to a string could just be as easy as

$free_options = implode(",", $_POST['free_options']);

to end up with a string containing all your array elements separated by a string.

https://www.php.net/manual/en/function.implode.php

1 Like

I would recommend to store actual arrays in a column of type text[], or even better use a JSON string so you can handle individual values via the native JSON capabilities of your database engine. But i would prefer a normalized table structure.

1 Like

What line of your script is throwing the array to string conversion?

It was the foreach loop that was causing it. :crazy_face: Thank you though.

If that’s why you want to put multiple values into a single column, then I suspect you will come to regret that when you see how much more complex it is to use the information you stored. There may be a very small number of cases where stuffing multiple values into one column won’t cause you any problems, but generally, it is better to either have a column for each value, or a separate linked table.
Just out of interest, why would you prefer to do it that way?If you really, really want to do it, converting the array to a string could just be as easy as

$free_options = implode(",", $_POST['free_options']);

Thanks Droopsnoot. That has solved it. Been working on it for 2 days now and couldnt figure it out. Much appreciated. There are only a few options to go in currently. The reason i prefer it, is that i like the idea of it all being in one table and easy to call upon. Plus i’m not overly familiar with doing a form within a form at this stage (i.e submitting options to one table and remaining fields to another table). Is separating it a much better option?

I would recommend to store actual arrays in a column of type text[] , or even better use a JSON string so you can handle individual values via the native JSON capabilities of your database engine. But i would prefer a normalized table structure.

Thanks Chorn, at the moment it’s set to Varchar. What benefits do text over vs Varchar? I haven’t ventured into JSON yet. Is there an advantage to JSON?

Thanks again all. Really appreciate the help.:+1:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.