用两个foreach更新表

时间:2016-03-29 17:56:17

标签: php arrays pdo foreach

我有两张桌子:

表格图片

if __name__ == "__main__"

表格POSTS

id_images | dir_image | post_id
     1      image1.jpg   1
     2      image2.jpg   1
     3      image3.jpg   1
     4      image4.jpg   1
     5      image5.jpg   2
     6      image6.jpg   2
     7      image7.jpg   2
     8      image8.jpg   2

PHP

id_post   |          slug        |   title
 1           title_post            Title Post 
 2           title_post_2          Title Post 2

变量 public function update($idsimages, $dir_images, $idPost, $title, $slug) { try { $stmt = $this->db->prepare("UPDATE posts SET title = :title, slug = :slug, WHERE id_post=:id_post"); $stmt->bindParam(":id_post",$idPost); $stmt->bindParam(":title",$title); $stmt->bindParam(":slug",$slug); $stmt->execute(); $stmt = $this->db->prepare("UPDATE images SET dir_images = :dir_images WHERE id_images = :id_images"); foreach ($idsimages as $idsimage) { foreach ($dir_images as $item){ $stmt->bindParam(":id_images", $idsimage); $stmt->bindParam(":dir_images", $item); } //end foreach dir_images $stmt->execute(); } // end foreach idsimages return true; } catch(PDOException $e) { echo $e->getMessage(); return false; } } $idsimages

的ID数组

变量$id_images是一个url数组,应该根据数组$dir_images

的id进行更新

使用上面的PHP代码正确更新帖子表的标题,但不更新表格图像

PRINTS

print_r($ idsimages);

$idsimages

print_r($ dir_images);

Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 )

如果我在函数中插入echo:

Array ( [0] => uploads/images/image5.jpg [1] => uploads/images/image6.jpg [2] => uploads/images/image7.jpg [3] => uploads/images/image8.jpg)

返回:

$stmt->bindParam(":id_images", $idsimage);
echo $idsimage;
$stmt->bindParam(":dir_images", $item); 
echo $item;

1 个答案:

答案 0 :(得分:1)

你需要这样的东西:

li