0

I have some code that worked perfectly a few hours ago, and for some reason it is nbow totally useless.

I have scoured the web for hours trying to work out why I am getting a Notice: array to string conversion error.

On a previous page I post and array of team names to the page where the error message is created.

If I var_dump the array (which is called $teams) all the data is there, every team name in an array.

array(1) { [0]=> array(12) { [0]=> string(6) "Team a" [1]=> string(6) "Team a" [2]=> string(6) "Team a" [3]=> string(6) "Team b" [4]=> string(6) "Team b" [5]=> string(6) "Team b" [6]=> string(6) "Team c" [7]=> string(6) "Team c" [8]=> string(6) "Team c" [9]=> string(6) "Team d" [10]=> string(6) "Team d" [11]=> string(6) "Team d" } }

Note, the team names are repeated as this is a fixture list, so each team plays each team.

However, the array to string conversion error is occuring in my query.

So, at the top of the page I create an array from the data posted like this...

$team[]=$_POST['team'];

then in my query I run a for loop and run the query, summarised as follows;

$count=count($team);
for($i=0;$i<$count;$i++ {
$query=$database->query("UPDATE pool_a SET ......where team_name='$team[$i]')"

I am so confused, I did a bit of editing earlier to try to add further queries to produce tables for pool_b, pool_c etc but I scrapped them and I am sure the code is as it was this morning.

Any help would be much appreciated.

Thanks

2
  • It appears that there is a value inside $team that is also an array. Can you edit your answer with the var_dump of $team? Commented Jan 16, 2014 at 21:01
  • yep just added the var_dump Commented Jan 16, 2014 at 21:04

1 Answer 1

1

Based on the var_dump() you posted, it looks like you array is actually located at $team[0].

It looks like you need to change this:

$team[] = $_POST['team'];

to this:

$team = $_POST['team'];

Assigning to $team[] in essence gave you an extra array wrapper around your data.

Since you also have duplicate values in the array, before querying the database you should reduce array only to unique values.

$team = array_unique($team);

You would be best served using a single query with all your items in it:

$query = "UPDATE pool_a SET ... WHERE team_name IN('" . implode("','", $team) . "')";
$database->query($query);
Sign up to request clarification or add additional context in comments.

6 Comments

@user2933231 Just edited my answer. It looks like you data is in $teams[0]
doesn't taking away the [] mean it won't be treated as an array?
i am now getting invalid offset errors for each iteration of that line in the query loop
@user2933231 If the posted data is already in array format, you just want to assign the post value directly to the array. Try var_dump($_POST['team']) to verify this. Also, you should not be querying in loop. Get unique values from array (since there are duplicates) and make single DB query as shown in my answer.
Vulnerable to SQL-injection.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.