Php 更新foreach循环内部don';我不能工作超过一次
我对mysql更新查询有问题。 我的代码是:Php 更新foreach循环内部don';我不能工作超过一次,php,mysql,arrays,Php,Mysql,Arrays,我对mysql更新查询有问题。 我的代码是: public function update($idp, $ids){ $sql = "UPDATE table SET idp = ? WHERE ids = ?"; $atmt = $this->db->prepare($sql); $atmt->bindParam(1, $idp, PDO::PARAM_INT); $atmt->bindParam(2, $ids, PDO::PARAM_
public function update($idp, $ids){
$sql = "UPDATE table SET idp = ? WHERE ids = ?";
$atmt = $this->db->prepare($sql);
$atmt->bindParam(1, $idp, PDO::PARAM_INT);
$atmt->bindParam(2, $ids, PDO::PARAM_INT);
$atmt->execute();
return 1;
}
我的数据数组是:
[0]=>
array(2) {
["id_s"]=>
int(2)
["id_p"]=>
int(111)
}
[1]=>
array(2) {
["id_s"]=>
int(3)
["id_p"]=>
int(175)
}
[2]=>
array(2) {
["id_s"]=>
int(5)
["id_p"]=>
int(25)
}
[3]=>
array(2) {
["id_s"]=>
int(7)
["id_p"]=>
int(127)
}
...
遍历数组的元素并调用update方法:
foreach($array as $index => $val){
$idp = $id[$index]['id_p'];
$ids = $id[$index]['id_s'];
$admin->update($idp, $ids);
}
我想要一张像tihs这样的桌子:
ids | idp | other column
1 5 ...
3 97 ...
3 97 ...
4 32 ...
5 178 ...
5 178 ...
5 178 ...
然而,对于foreach循环,我只更新第一条记录;
数组对所有值都没有问题,似乎参数绑定不起作用。
有解决办法吗?
谢谢 您正在返回在foreach循环中调用的更新函数,该函数将终止循环什么是
$array
和$id
?@PaulSpiegel它们都是int。foreach($array..
和$id[$index]
-它们不能是int
。或者它们不应该是。对不起,我的意思是数组的所有值都是int,id\u p和id\u p。