2016-03-29 9 views
0

Ich habe zwei Tabellen:Update-Tabelle mit zwei foreach

Tabelle IMAGES

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 

Tabelle BEITRÄGE

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

PHP

 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; 
       } 
       } 

Die Variable $idsimages ist ein Array von Ids die $id_images Spalte

Die Variable $dir_images ist ein Array von URLs, die oben richtig $idsimages

Mit PHP-Code entsprechend der ID des Arrays aktualisiert werden sollte aktualisiert der Titel der Post-Tabelle, wird aber nicht aktualisiert die Tabellen Bilder


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) 

wenn ich einfügen Echo in der Funktion:

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

Rückkehr:

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 
+0

Die bindParam ist in Ihrem inneren Schleife bei jeder Iteration überschrieben werden . – noahnu

+0

Wenn Sie param: dir_image binden, sollte es nicht sein: dir_images? –

+0

@dnFer Thank's es war nur ein Tippfehler in meiner Frage Ich korrigierte – Gisele

Antwort

1

Sie brauchen etwas wie folgt aus:

$stmt = $this->db->prepare("UPDATE images SET 
    dir_images = :dir_images 
    WHERE id_images = :id_images"); 

foreach ($idsimages as $key => $idsimage) { 
    $stmt->bindParam(":id_images", $idsimage); 
    $stmt->bindParam(":dir_images", $dir_images[$key]);    

    $stmt->execute(); 
} // end foreach idsimages 
+0

Danke, aber es funktioniert nicht für mich – Gisele

+0

aber '$ idsimage' Es ist auch ein Array – Gisele

+0

Wie sieht' $ idsimage' und '$ dir_images'? – mnv