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.
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.
… 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.
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?