0
Hello,
public function update($idsimages, $dir_images)
{
try {
$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;
}
}
The variables $idsimages
(contains the ids that will be updated) and $dir_images
(contains urls) are arrays, the two arrays have the same amount of keys
It turns out that it is not doing the updade, I placed echo in the two variables and after Submit prints repeatedly, several times, the images and ids.
I tried to change several times the foreachs and also insert if isset not to repeat, but I could not
PRINTS
print_r ($idsimages);
Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 )
print_r ($dir_images);
Array ( [0] => uploads/images/image5.jpg [1] => uploads/images/image6.jpg [2] => uploads/images/image7.jpg [3] => uploads/images/image8.jpg)
Insert echo into function:
$stmt->bindParam(":id_images", $idsimage);
echo $idsimage;
$stmt->bindParam(":dir_images", $item);
echo $item;
returns:
5 image5.PNG 5 image6.jpg 5 image6.jpg 5 image6.jpg
6 image5.PNG 6 image6.jpg 6 image6.jpg 6 image6.jpg
7 image5.PNG 7 image6.jpg 7 image6.jpg 7 image6.jpg
8 image5.PNG 8 image6.jpg 8 image6.jpg 8 image6.jpg
vc only binds the last element of the internal foreach and executes the query N times with the same value. If you have an error in the query see the return of
$stmt->errorInfo():
– rray
Thanks @rray any idea how to solve? The query does not return any errors even with your suggestion
– Gisele