Can't resolve "Notice: Array to string conversion"

I get this error because I am inserting an array in the line rather than the array’s value. But I don’t know how to get the value out at this stage.

The error line is at:
$s->bindValue(‘:brand’, $_POST[‘formNoAds’]);

The array is called ‘formNoAds’.

try
{
$sql_adv = "INSERT INTO nonmonthly_adv_" . $adv_i . " SET
size_rob = :size_rob,
size_premium = :size_premium,
brand = :brand,
name = :name,
month = :month,
year = :year
";

// bindValue prevents injection attacks
 $s = $pdo->prepare($sql_adv);
 $s->bindValue(':size_rob', $_POST['size_rob']);
 $s->bindValue(':size_premium', $_POST['size_premium']);
 $s->bindValue(':brand', $_POST['formNoAds']); // won't work because formNoAds is an array
 $s->bindValue(':name', $_POST['name']);
 $s->bindValue(':month', $_POST['month']);
 $s->bindValue(':year', $_POST['year']);
 $s->execute();
 }

So when the code executes, it puts the word “Array” in the brand field in the table rather than the name of the brand that’s in the array. The rest of the fields fill in correctly.

If I replace ‘formNoAds’ with ‘brand’ then the code does not insert any information in the fields.

It’s up to you what value you want to put in there.


$s->bindValue(':brand', reset( $_POST['formNoAds'] ));

will give you the first element. So $_POST[‘formNoAds’] is an array. What does that mean to your script? Is it not supposed to be an array?

Or (to tack on QMonkey’s thought), should it be a comma separated list of values? In which case using [fphp]implode[/fphp] would make perfect sense.

the trick here is in your opening line. “The array’s value”. The array… doesnt have a value. It has lots of them. Which, is kind of the definition of an array. So Array.toString = “Array”, always, since PHP hasnt been told which value of the array’s…array… you’re looking for. (Note: An empty array will still return “Array” as it’s toString evaluation. After all, it’s still an array.)

As the others have said, select a singular value out of the array (reset(), $array[0], etc), or implode the array if you want all of the values in said array tacked together.

The obvious thing to do is to put ‘brand’ in place of ‘formNoAds’:

$s->bindValue(‘:brand’, reset( $_POST[‘formNoAds’] ));

… and the value of ‘brand’ will change/update as it loops over and over until the array is finished. However, ‘brand’ halts the script and generates errors; nothing is written to the database at all. This is where I’m confused. I do understand that the array is a bunch of values; conversely, confusingly, putting the array name there allows all the other fields to be populated instead of none of them. I’ll check the answers here carefully and see what they do for me.

Here is how the array begins:

foreach ($_POST[‘formNoAds’] as $brand => $value)
{
if ($value == 1)
{

if ($brand == "The Argo") { $adv_i = "ta";}

Then it stops the list just above the ‘try’ block.

Thanks for responding!

So why does brand work in the try block:
brand = :brand,

but not in the bindValue block:
$s->bindValue(‘:brand’, $_POST[‘brand’]);

Please post a lot more of the surrounding code, I’m not sure I’m following where the try block is located compared to the foreach you just described.

<?php
require_once ('cnx.php'); 

foreach ($_POST['formNoAds'] as $brand => $value)
{
if ($value == 1)
{

if ($brand == "The Argo") { $adv_i = "ta";}
    if ($brand == "123abc") { $adv_i = "123";}
}

try 
{
$sql_adv = "INSERT INTO nonmonthly_adv_" . $adv_i . " SET
size_rob = :size_rob,
size_premium = :size_premium,
brand = :brand,
name = :name,
month = :month,
year = :year
";

// bindValue prevents injection attacks
 $s = $pdo->prepare($sql_adv);
 $s->bindValue(':size_rob', $_POST['size_rob']);
 $s->bindValue(':size_premium', $_POST['size_premium']);
 $s->bindValue(':brand', $_POST['formNoAds']); // won't work because formNoAds is an array. "brand" here doesn't work either.
 $s->bindValue(':name', $_POST['name']);
 $s->bindValue(':month', $_POST['month']);
 $s->bindValue(':year', $_POST['year']);
 $s->execute();
 }

            catch (PDOException $e)
                {

You wouldn’t use $_POST[‘brand’], you’d use $brand, as that is what is defined in the foreach.

My opening post says, “If I replace ‘formNoAds’ with ‘brand’ then the code does not insert any information in the fields,” e.g., when i change:

$s->bindValue(‘:brand’, $_POST[‘formNoAds’]);

to

$s->bindValue(‘:brand’, $_POST[‘brand’]);

So did you mean a different syntax change?

Thanks.

No one here can know what piece of data you want to put into that table. You have to be the one to know which input field has the data you want there. Is it $_POST[‘formNoAds’] or is it $_POST[‘brand’] or is it something else completely?

Yes, I mean
$s->bindValue(‘:brand’, $brand);

Perfect! That’s the missing piece I needed!

Thanks!