dsour68888 2014-01-16 20:57
浏览 42

数组值在查询中不起作用

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

  • 写回答

1条回答 默认 最新

  • doucang6914 2014-01-16 21:01
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数