Update table using PDO with 2 foreachs

Asked

Viewed 62 times

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():

  • Thanks @rray any idea how to solve? The query does not return any errors even with your suggestion

1 answer

0

It seems better to change the logic, ie perform the update within the internal foreach and not as described in the comment.

foreach ($idsimages as $idsimage) { 
   foreach ($dir_images as $item){
      if(!$stmt->execute(array(':id_images' => $idsimage, ':dir_images' => $item))){
         echo '<pre>';
         print_r($stmt->errorInfo());
      }
    } //end foreach dir_images 
} 
  • no change in result, still not saving, no errors too

  • @Gisele autocommit is on or not? after if add $this->db->commit() see if you save the change.

  • I honestly didn’t even know I had this in mysql, I just searched I’m trying to understand

  • @Gisele If it is off, the change is an update/insert or delete only takes effect if there is a commit.

  • I am working with the files directly from hostgator hosting (shared), as I do to see if this enabled?

  • With the $this->db->commit() after the if didn’t work? maybe you gave an error but it wasn’t displayed, put these two lines at the beginning of the file, ini_set('display_errors', true); error_reporting(E_ALL); @Gisele

  • Insert and select this working normally, to display the errors was already enabled, with the commit after if returns There is no active transaction,

  • @Gisele if you copy and put this query in the bank(and replace the clear values) works?

  • yes, it worked normal

  • @Gisele does one last thing again that file to the production host.

  • but it is already in production, with restricted access

  • @Gisele has no indication of what the mistake is.

  • I don’t know, I think the logic is really wrong, you can use do while in that case?

  • see the results (prints) of variables for the code of my question

  • @Gisele because you have both foreachs?

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.