使用Foreach使用数组更新MySQL表 [英] Update MySQL table with an Array using Foreach
问题描述
这让我坐便不便,请帮忙.
This is driving me potty so please help.
我正在尝试使用数组更新Mysql表.
I am trying to update a Mysql table with an array.
类似的东西
$a = array('1', '2', '3');
foreach($a as $id){
mysql_query("UPDATE table SET id = '$id' WHERE column = 'something'") or die(mysql_error());
}
因此,更新后,id列应具有值1、2、3 而是用1,1,1
So after the update the id column should have values 1, 2, 3 Instead it updates with 1, 1, 1
不完全是我想要的.
有人可以显示我在做什么吗?
Can someone please showing what I am doing wrong.
谢谢.
推荐答案
您是否在真实代码中更改了where
语句?现在,您将覆盖column ='something'的每一行,这意味着每一行都将被更新,并最终得到相同的内容.
Do you change your where
-statement in the real code? Now you are overwriting every row where column = 'something' which would means every row would be updated every time and end up with the same content.
回答评论
那么,您将需要一个非静态的WHERE
语句.您可以在我的帖子中做类似的编辑...
Well, you would need a non-static WHERE
-statement for this. You could do something like the edit in my post...
$a = array('1' => 'something1', '2' => 'something2', '3' => 'something3');
foreach($a as $id => $where){
mysql_query("UPDATE table SET id = '$id' WHERE column = '$where'") or die(mysql_error());
}
这篇关于使用Foreach使用数组更新MySQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!