该update
语句用于更新数据而不是插入数据。因此,通过更新,您只能更改表上已有的内容,不会创建任何新内容。
如果要插入新数据,则应使用insert
语句。
使用您发布的命令,您对数据库说
嘿,男人用表所在的player
值更改属性'".$newPlayer[$k]."'
id
playear
'".$id."'"
因此,正如评论中所讨论的,这里对您来说是一个更好的解决方案:
update players p,
(select id, player, substring_index( substring_index(lst, ',', idx), ',', -1) as pl
from (select p.*, @number := @number+1 as idx, lst
from players p,
(select @number := 0 as nb) n,
(select 'i,j,k,l,m,n,o,p' as lst) lst) x ) a
set p.player = a.pl
where p.id = a.id
and p.player = a.player
但是对于 PHP,你必须将你的数组$newPlayer
作为一个字符串,由 a 分隔,中间,
没有空格。
像这样的东西:
$newNames = implode(",",$newPlayer);
$que = "update players p,
(select id, player, substring_index( substring_index(lst, ',', idx), ',', -1) as pl
from (select p.*, @number := @number+1 as idx, lst
from players p,
(select @number := 0 as nb) n,
(select '$newNames' as lst) lst) x ) a
set p.player = a.pl
where p.id = a.id
and p.player = a.player";
$db_con->exec($que);
有了这个,你就不需要for