Convert to PDO Statement

Hi

I have an existing simple PHP/MySQL query that works correctly. I am in the process of updating an old website converting all the queries to PDO statements. Most have now been changed succesfully but this one will not work.



################## EXISTING QUERY ##########################

$r=mysql_query("insert into pics(id) values (''" . $_REQUEST['id'] . "')");
$pid=mysql_insert_id();


################## PDO ##################################

try {
                 

$r = "INSERT INTO `pics`(`id`) VALUES (:id)";
$q1 = $dbh->prepare($r);
$q1->bindvalue(':id', $_REQUEST['id'], PDO::PARAM_INT);
$q1->execute();

$pid = $dbh->lastInsertId(); // GET LAST pid FROM DB

} catch (PDOException $e) {
    print "Error Inserting New Image!: " . $e->getMessage() . "<br/>";
    die();
} 

The value of $pid has to be passed in a URL to the next page but it has no value? I suspect the problem lies with lastInsertId().

The pid column in the DB is an integer, auto incremental and the primary key.

The old query works fine so I must have an error in my PDO I guess.

Thanks in advance.

Hi Colin,

not sure but there might be a couple of things that you might try:

  1. Do the INSERT statement without the backticks ` as in your original statement.
  2. The original statements appears to be inserting a string rather than an integer value
  3. Most likely a typo but your original INSERT contains two single quotes ’ at the beginning.
  4. What is the auto_incremented key in this table?
  5. When using PDO I use $ql->execute(array(“id” => $_REQUEST[‘id’])) which always works for me.

I now have:



try {
                 

$r = "INSERT INTO pics (id) VALUES (:id)";
$q1 = $dbh->prepare($r);
$q1->bindvalue(':id', $_REQUEST['id']);
$q1->execute();

$pid = $dbh->lastInsertId(); // GET LAST pid FROM DB

} catch (PDOException $e) {
    print "Error Inserting New Image!: " . $e->getMessage() . "<br/>";
    die();
}


Still does not give me a value for $pid I’m afraid.

Yes that was a typo in original.

pid is the auto incremented primary key in the table.

Thanks for your help on this.

Probably another type but bindValue? Have you also tested the value of $_REQUEST[‘id’] to make sure that this is an actual value?

Yes $_REQUEST[‘id’] has a correct value

What is the problem with bindValue?

In your code you have put $q1->bindvalue(‘:id’, $_REQUEST[‘id’]); should this be $q1->bindValue(‘:id’, $_REQUEST[‘id’]);

Also make sure that the table does have the auto_increment (this may have been taken off for some reason)?

Sounds as if colinhughes is having the data saved ok, and surely there is a correctly formed auto-increment value being saved … (you are using mysql in any case?)

try this in case the transfer of $pid to the GET string is going wrong elsewhere…


$pid = $dbh->lastInsertId(); // GET LAST pid FROM DB
echo $pid; // throw it onto the page or into an error log

else var_dump($dbh) and see if there any clues there …

EDIT ioops, wrong name …




try {
                 

$r = "INSERT INTO pics (id) VALUES (:id)";
$q1 = $dbh->prepare($r);
$q1->bindvalue(':id', $_REQUEST['id']);
$q1->execute();

$vari = var_dump($dbh);

$pid = $dbh->lastInsertId(); // GET LAST pid FROM DB

} catch (PDOException $e) {
    print "Error Inserting New Image!: " . $e->getMessage() . "<br/>";
    die();
} 

echo $vari;



echo $pid returns empty

var_dump returns empty

$_REQUEST[‘id’] has expected value

var_dump($dbh); returns: object(PDO)#1 (0) {}

If you are absolutely sure the new row is being created, then you could try this instead:


$pid = PDO::getLastInsertId();
echo $pid;

OK so here is complete code:

&lt;?php
  if (isset($_POST['sbmt'])):
    
    $cat_id=$_REQUEST['cat_id'];
     $propertyref=$_REQUEST['propertyref'];
     
    
            for($i=0;$i&lt;count($_FILES['foto']['tmp_name']);$i++):
            if($_FILES['foto']['tmp_name']['$i']):
            
            
            ###### OLD INSERT QUERY #######
            
            
            //$r=mysql_query("insert into pics(id) values ('" . $_REQUEST['id'] . "')");
              
              //$pid=mysql_insert_id();
               
           ######## NEW PDO INSERT QUERY ##########
         
         
             try {
                 

$r = "INSERT INTO `pics`(`id`) VALUES (:id)";
$q1 = $dbh->prepare($r);
$q1->bindvalue(':id', $_REQUEST['id']);
$q1->execute();

$pid = $dbh->lastInsertId(); // GET LAST pid FROM DB

} catch (PDOException $e) {
    print "Error Inserting New Image!: " . $e->getMessage() . "<br/>";
    die();
}  
               
         
          
              
               $e=explode(".",$_FILES['foto']['name'][$i]);
               $ext=substr($_FILES['foto']['name'][$i],-3,3); 
               $size=$foto_size;
               $ext=strtolower($e[1]);
               if($ext=="peg"){$ext="jpg";}
               
               
               copy($_FILES['foto']['tmp_name']['$i'],"images/" . $pid . "." . $ext);
               copy($_FILES['foto']['tmp_name']['$i'],"images/thumb-" . $pid . "." . $ext);
         

/*         
                     ###### OLD UPDATE QUERY #######
         
         //mysql_query("update pics set file='" .  $pid . "." . $ext . "',thumb='thumb-" . $pid . "." . $ext . "' where pid=" . $pid);    
         
                    ######## NEW PDO UPDATE QUERY ##########
         
$file= $pid . "." . $ext ;

$thumb="thumb-" . $pid . "." . $ext;     

 try { 
      $r = "UPDATE `pics` SET `file`= ?, `thumb`= ? WHERE `pid`=" . $pid."";
      $q1 = $dbh-&gt;prepare($r);
      $q1-&gt;bindvalue(1, $file);
      $q1-&gt;bindvalue(2, $thumb);
      $q1-&gt;execute();
 
} catch (PDOException $e) {
    print "Error Updating Image!: " . $e-&gt;getMessage() . "&lt;br/&gt;";
    die();
}

*/

               $nw=145;$nh=133; //Size in pixels of thumbnail
               $img=$tpath . "/thumb-" . $pid . "." . $ext;
               $dimensions = GetImageSize($img);
               $thname = $img;
               $w=$dimensions['0'];
               $h=$dimensions['1'];
               
               if($ext=="jpg"):
                   $img2 = ImageCreateFromJpeg($img);
               elseif($ext=="gif"):
                  $img2 = ImageCreateFromGif($img);
               elseif($ext=="png"): 
                  $img2 = ImageCreateFromPng($img);
               endif;    
               
               $thumb=ImageCreateTrueColor($nw,$nh);
               $wm = $w/$nw;
               $hm = $h/$nh;
     
                $h_height = $nh/2;
               $w_height = $nw/2;
               if($w &gt; $h):
               
                   $adjusted_width = $w/$hm;
                   $half_width = $adjusted_width / 2;
                   $int_width = $half_width - $w_height;
                   ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); 
                   if($ext=="jpg"):
                        ImageJPEG($thumb,$thname,95); 
                   elseif($ext=="gif"):
                        ImageGIF($thumb,$thname,95); 
                   elseif($ext=="png"): 
                        @ImagePNG($thumb,$thname,9); 
                   endif;    
                   
                
                elseif(($w &lt; $h) || ($w == $h)):
                
                    $adjusted_height = $h / $wm;
                    $half_height = $adjusted_height / 2;
                    $int_height = $half_height - $h_height;
                    ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
                    
                    
                    if($ext=="jpg"):
                        ImageJPEG($thumb,$thname,95); 
                    elseif($ext=="gif"):
                       ImageGIF($thumb,$thname,95); 
                    elseif($ext=="png"): 
                       ImagePNG($thumb,$thname,9); 


                    endif;    
                
                else:
                
                      ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h); 
                    
                        
                    if($ext=="jpg"):
                       ImageJPEG($thumb,$thname,95); 
                    elseif($ext=="gif"):
                       ImageGIF($thumb,$thname,95); 
                    elseif($ext=="png"): 
                         ImagePNG($thumb,$thname,9); 
                    endif;
                    
                        
                 endif;
                 
                 
                 imagedestroy($img2);
                 
                   $target=550; //Size in pixels
                  

                   $img=$tpath . "/" . $pid . "." . $ext;
                   $dimensions = GetImageSize($img);
                   $thname = $img;
                   $w=$dimensions['0'];
                   $h=$dimensions['1'];
                   $width=$w;
                   $height=$h;
                   
                   
                   if(($width&gt;$target && $height&gt;$target) || $width&gt;$target):
                       $cent=100-round((($width-$target)/$width)*100);
                       $width=round($width*$cent/100);
                       $height=round($height*$cent/100);
                   elseif($height&gt;$target):
                       $cent=100-round((($height-$target)/$height)*100);
                       $width=round($width*$cent/100);
                       $height=round($height*$cent/100);
                   endif;
                   
                   
                   if(($width&gt;$target && $height&gt;$target)|| $width&gt;$target):
                        $cent=100-round((($width-$target)/$width)*100);
                        $width =round($width*$cent/100);
                        $height=round($height*$cent/100);
                   elseif($height&gt;$target):
                        $cent=100-round((($height-$target)/$height)*100);
                        $width =round($width*$cent/100);
                        $height=round($height*$cent/100);
                   endif;
                   
                   
                   $nw=$width;$nh=$height;
                   
                   
                   if($ext=="jpg"):
                       $img2 = ImageCreateFromJpeg($img);
                   elseif($ext=="gif"):
                      $img2 = ImageCreateFromGif($img);
                   elseif($ext=="png"): 
                      $img2 = ImageCreateFromPng($img);
                   endif;    
                   
                   
                   $thumb=ImageCreateTrueColor($nw,$nh);
                   $wm = $w/$nw;
                   $hm = $h/$nh;
         
                    $h_height = $nh/2;
                   $w_height = $nw/2;
                   
                   
                   if($w &gt; $h):
                   
                       $adjusted_width = $w/$hm;
                       $half_width = $adjusted_width / 2;
                       $int_width = $half_width - $w_height;
                       ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); 
                       
                       
                       if($ext=="jpg"):
                            ImageJPEG($thumb,$thname,95); 
                       elseif($ext=="gif"):
                            ImageGIF($thumb,$thname,95); 
                       elseif($ext=="png"): 
                            ImagePNG($thumb,$thname,9); 
                       endif;    
                       
                       
                    
                    elseif(($w &lt; $h) || ($w == $h)):
                   
                        $adjusted_height = $h / $wm;
                        $half_height = $adjusted_height / 2;
                        $int_height = $half_height - $h_height;
                        ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
                        
                        
                        if($ext=="jpg"):
                            ImageJPEG($thumb,$thname,95); 
                        elseif($ext=="gif"):
                           ImageGIF($thumb,$thname,95); 
                        elseif($ext=="png"): 
                           ImagePNG($thumb,$thname,9); 
                        endif;
                        
                            
                    
                    else:
                    
                          ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h); 
                        
                            
                        if($ext=="jpg"):
                           ImageJPEG($thumb,$thname,95); 
                        elseif($ext=="gif"):
                           ImageGIF($thumb,$thname,95); 
                        elseif($ext=="png"): 
                             ImagePNG($thumb,$thname,9); 
                        endif;
                        
                            
                     endif;
                     
                     imagedestroy($img2);
                     
                endif;
                
            endfor;
            


                echo"&lt;script&gt;alert('Image successfully Updated');window.location.href='update-images.php?id=" . $_REQUEST['id'] . "&amp;cat_id=" . $_REQUEST['cat_id'] . "&amp;propertyref=" . $_REQUEST['propertyref'] . "';&lt;/script&gt;";        
         endif;
         

           
         
          
   
 include('nav.php');
      
         
    echo "&lt;h4 class='offset-by-eleven' id ='highlight'&gt;Adding New Images&lt;/h4&gt;&lt;/div&gt;";  

 echo ”;
print_r($_FILES['foto']);
/* RESULT OF PRINT ARRAY 

Array
(
    [name] =&gt; Array
        (
            [0] =&gt; buyers.jpg
            [1] =&gt; 
            [2] =&gt; 
            [3] =&gt; 
            [4] =&gt; 
            [5] =&gt; contact1.png
            [6] =&gt; 
            [7] =&gt; 
            [8] =&gt; 
            [9] =&gt; 
        )

    [type] =&gt; Array
        (
            [0] =&gt; image/jpeg
            [1] =&gt; 
            [2] =&gt; 
            [3] =&gt; 
            [4] =&gt; 
            [5] =&gt; image/png
            [6] =&gt; 
            [7] =&gt; 
            [8] =&gt; 
            [9] =&gt; 
        )

    [tmp_name] =&gt; Array
        (
            [0] =&gt; C:\\Windows\\Temp\\php232A.tmp
            [1] =&gt; 
            [2] =&gt; 
            [3] =&gt; 
            [4] =&gt; 
            [5] =&gt; C:\\Windows\\Temp\\php232B.tmp
            [6] =&gt; 
            [7] =&gt; 
            [8] =&gt; 
            [9] =&gt; 
        )

    [error] =&gt; Array
        (
            [0] =&gt; 0
            [1] =&gt; 4
            [2] =&gt; 4
            [3] =&gt; 4
            [4] =&gt; 4
            [5] =&gt; 0
            [6] =&gt; 4
            [7] =&gt; 4
            [8] =&gt; 4
            [9] =&gt; 4
        )

    [size] =&gt; Array
        (
            [0] =&gt; 13781
            [1] =&gt; 0
            [2] =&gt; 0
            [3] =&gt; 0
            [4] =&gt; 0
            [5] =&gt; 75298
            [6] =&gt; 0
            [7] =&gt; 0
            [8] =&gt; 0
            [9] =&gt; 0
        )

)

*/
echo ”;    




  ?&gt;
  
  &lt;!-- Add Images --&gt;
  
  &lt;div class="add-image" &gt; &lt;!-- START FORM DIV --&gt;
  

    
    &lt;form method="post" name="add" action="&lt;?php echo $_SERVER['PHP_SELF']?&gt;?propertyref=&lt;?php echo $_REQUEST['propertyref'];?&gt;" enctype="multipart/form-data"&gt;
      &lt;input type="hidden" name="id" value="&lt;?php echo $_REQUEST['id']?&gt;"&gt;
      &lt;input type="hidden" name="cat_id" value="&lt;?php echo $_REQUEST['cat_id']?&gt;"&gt;
      &lt;fieldset class="add-prop"&gt;
        &lt;legend class="sixteen columns"&gt;Adding images&lt;/legend&gt;

        
        &lt;?php
      for($i=1;$i&lt;=10;$i++):
          echo"&lt;input type='file' class='image-upload' name='foto[]' &gt;\
";      
      endfor;
      ?&gt;
      
        &lt;br /&gt;
        &lt;input type="hidden" name="source" value="&lt;?php echo $source?&gt;"&gt;
        &lt;input type="hidden" name="thumb" value="&lt;?php echo $thumb?&gt;"&gt;
        &lt;input type="submit"  class="image-upload" name="sbmt" value="Image Upload"&gt;
        &lt;br /&gt;
      &lt;/fieldset&gt;
    &lt;/form&gt;
    

  &lt;/div&gt;

Appreciate your assistance

instead of
$q1 = $dbh->prepare($r);
$q1->bindvalue(‘:id’, $_REQUEST[‘id’]);

try

$q1 = $dbh->prepare($r);
$q1->bindvalue(‘:id’, (int)$_REQUEST[‘id’], PDO::PARAM_INT);



$q1 = $dbh->prepare($r);
$q1->bindvalue(':id', (int)$_REQUEST['id'], PDO::PARAM_INT); 
$q1->execute();
$count = $q1->rowCount();

$pid = $dbh->lastInsertId(); // GET LAST pid FROM DB


$count has no value

OK so this is where i am at with this and getting rather frustrated with it.


&lt;?php
  if (isset($_POST['sbmt'])):
    
    $cat_id=$_REQUEST['cat_id'];
     $propertyref=$_REQUEST['propertyref'];
     
    
            for($i=0;$i&lt;count($_FILES['foto']['tmp_name']);$i++):
            if($_FILES['foto']['tmp_name']['$i']):
            
            
            ###### OLD INSERT QUERY #######
            
            
            //$r=mysql_query("insert into pics(id) values ('" . $_REQUEST['id'] . "')");
              
              //$pid=mysql_insert_id();
               
           ######## NEW PDO INSERT QUERY ##########
         
       // insert new id into db, pid auto incrementally generated
   
             try {
                 

$r = "INSERT INTO `pics`(`id`) VALUES (:id)";
$q1 = $dbh-&gt;prepare($r);
$q1-&gt;bindvalue(':id', $_REQUEST['id']);
$q1-&gt;execute();

$pid = $dbh-&gt;lastInsertId(); // GET LAST pid FROM DB

} catch (PDOException $e) {
    print "Error Inserting New Image!: " . $e-&gt;getMessage() . "&lt;br/&gt;";
    die();
}  
               
         
          
              
               $e=explode(".",$_FILES['foto']['name'][$i]);
               $ext=substr($_FILES['foto']['name'][$i],-3,3); 
               $size=$foto_size;
               $ext=strtolower($e[1]);
               if($ext=="peg"){$ext="jpg";}
               
               
               copy($_FILES['foto']['tmp_name']['$i'],"images/" . $pid . "." . $ext);
               copy($_FILES['foto']['tmp_name']['$i'],"images/thumb-" . $pid . "." . $ext);
         

/*         
                     ###### OLD UPDATE QUERY #######
         
         //mysql_query("update pics set file='" .  $pid . "." . $ext . "',thumb='thumb-" . $pid . "." . $ext . "' where pid=" . $pid); 

*/   
         
                    ######## NEW PDO UPDATE QUERY ##########
         
  //update db using new pid and ext from above

$file = $pid . "." . $ext ;

$thumb = "thumb-" . $pid . "." . $ext;    

 try { 
      $r2 = "UPDATE  pics SET  file = :file, thumb = :thumb WHERE  pid =:pid";
      $q2 = $dbh-&gt;prepare($r2);
      $q2-&gt;bindParam(":file", $file);
      $q2-&gt;bindParam(":thumb", $thumb);
      $q2-&gt;bindParam(":pid", $pid);
      $q2-&gt;execute();
      $count2 = $q2-&gt;rowCount();
 
} catch (PDOException $e) {
    print "Error Updating Image!: " . $e-&gt;getMessage() . "&lt;br/&gt;";
    die();
}



               $nw=145;$nh=133; //Size in pixels of thumbnail
               $img=$tpath . "/thumb-" . $pid . "." . $ext;
               $dimensions = GetImageSize($img);
               $thname = $img;
               $w=$dimensions['0'];
               $h=$dimensions['1'];
               
               if($ext=="jpg"):
                   $img2 = ImageCreateFromJpeg($img);
               elseif($ext=="gif"):
                  $img2 = ImageCreateFromGif($img);
               elseif($ext=="png"): 
                  $img2 = ImageCreateFromPng($img);
               endif;    
               
               $thumb=ImageCreateTrueColor($nw,$nh);
               $wm = $w/$nw;
               $hm = $h/$nh;
     
                $h_height = $nh/2;
               $w_height = $nw/2;
               if($w &gt; $h):
               
                   $adjusted_width = $w/$hm;
                   $half_width = $adjusted_width / 2;
                   $int_width = $half_width - $w_height;
                   ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); 
                   if($ext=="jpg"):
                        ImageJPEG($thumb,$thname,95); 
                   elseif($ext=="gif"):
                        ImageGIF($thumb,$thname,95); 
                   elseif($ext=="png"): 
                        @ImagePNG($thumb,$thname,9); 
                   endif;    
                   
                
                elseif(($w &lt; $h) || ($w == $h)):
                
                    $adjusted_height = $h / $wm;
                    $half_height = $adjusted_height / 2;
                    $int_height = $half_height - $h_height;
                    ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
                    
                    
                    if($ext=="jpg"):
                        ImageJPEG($thumb,$thname,95); 
                    elseif($ext=="gif"):
                       ImageGIF($thumb,$thname,95); 
                    elseif($ext=="png"): 
                       ImagePNG($thumb,$thname,9); 


                    endif;    
                
                else:
                
                      ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h); 
                    
                        
                    if($ext=="jpg"):
                       ImageJPEG($thumb,$thname,95); 
                    elseif($ext=="gif"):
                       ImageGIF($thumb,$thname,95); 
                    elseif($ext=="png"): 
                         ImagePNG($thumb,$thname,9); 
                    endif;
                    
                        
                 endif;
                 
                 
                 imagedestroy($img2);
                 
                   $target=550; //Size in pixels
                  

                   $img=$tpath . "/" . $pid . "." . $ext;
                   $dimensions = GetImageSize($img);
                   $thname = $img;
                   $w=$dimensions['0'];
                   $h=$dimensions['1'];
                   $width=$w;
                   $height=$h;
                   
                   
                   if(($width&gt;$target && $height&gt;$target) || $width&gt;$target):
                       $cent=100-round((($width-$target)/$width)*100);
                       $width=round($width*$cent/100);
                       $height=round($height*$cent/100);
                   elseif($height&gt;$target):
                       $cent=100-round((($height-$target)/$height)*100);
                       $width=round($width*$cent/100);
                       $height=round($height*$cent/100);
                   endif;
                   
                   
                   if(($width&gt;$target && $height&gt;$target)|| $width&gt;$target):
                        $cent=100-round((($width-$target)/$width)*100);
                        $width =round($width*$cent/100);
                        $height=round($height*$cent/100);
                   elseif($height&gt;$target):
                        $cent=100-round((($height-$target)/$height)*100);
                        $width =round($width*$cent/100);
                        $height=round($height*$cent/100);
                   endif;
                   
                   
                   $nw=$width;$nh=$height;
                   
                   
                   if($ext=="jpg"):
                       $img2 = ImageCreateFromJpeg($img);
                   elseif($ext=="gif"):
                      $img2 = ImageCreateFromGif($img);
                   elseif($ext=="png"): 
                      $img2 = ImageCreateFromPng($img);
                   endif;    
                   
                   
                   $thumb=ImageCreateTrueColor($nw,$nh);
                   $wm = $w/$nw;
                   $hm = $h/$nh;
         
                    $h_height = $nh/2;
                   $w_height = $nw/2;
                   
                   
                   if($w &gt; $h):
                   
                       $adjusted_width = $w/$hm;
                       $half_width = $adjusted_width / 2;
                       $int_width = $half_width - $w_height;
                       ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); 
                       
                       
                       if($ext=="jpg"):
                            ImageJPEG($thumb,$thname,95); 
                       elseif($ext=="gif"):
                            ImageGIF($thumb,$thname,95); 
                       elseif($ext=="png"): 
                            ImagePNG($thumb,$thname,9); 
                       endif;    
                       
                       
                    
                    elseif(($w &lt; $h) || ($w == $h)):
                   
                        $adjusted_height = $h / $wm;
                        $half_height = $adjusted_height / 2;
                        $int_height = $half_height - $h_height;
                        ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
                        
                        
                        if($ext=="jpg"):
                            ImageJPEG($thumb,$thname,95); 
                        elseif($ext=="gif"):
                           ImageGIF($thumb,$thname,95); 
                        elseif($ext=="png"): 
                           ImagePNG($thumb,$thname,9); 
                        endif;
                        
                            
                    
                    else:
                    
                          ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h); 
                        
                            
                        if($ext=="jpg"):
                           ImageJPEG($thumb,$thname,95); 
                        elseif($ext=="gif"):
                           ImageGIF($thumb,$thname,95); 
                        elseif($ext=="png"): 
                             ImagePNG($thumb,$thname,9); 
                        endif;
                        
                            
                     endif;
                     
                     imagedestroy($img2);
                     
                endif;
                
            endfor;
            


                echo"&lt;script&gt;alert('Image successfully Updated');window.location.href='update-images.php?id=" . $_REQUEST['id'] . "&amp;cat_id=" . $_REQUEST['cat_id'] . "&amp;propertyref=" . $_REQUEST['propertyref'] . "';&lt;/script&gt;";        
         endif;
         

           
         
          
   
 include('nav.php');
      
         
    echo "&lt;h4 class='offset-by-eleven' id ='highlight'&gt;Adding New Images&lt;/h4&gt;&lt;/div&gt;";  

 echo ”;
print_r($_FILES['foto']);
/* RESULT OF PRINT ARRAY 

Array
(
    [name] =&gt; Array
        (
            [0] =&gt; buyers.jpg
            [1] =&gt; 
            [2] =&gt; 
            [3] =&gt; 
            [4] =&gt; 
            [5] =&gt; contact1.png
            [6] =&gt; 
            [7] =&gt; 
            [8] =&gt; 
            [9] =&gt; 
        )

    [type] =&gt; Array
        (
            [0] =&gt; image/jpeg
            [1] =&gt; 
            [2] =&gt; 
            [3] =&gt; 
            [4] =&gt; 
            [5] =&gt; image/png
            [6] =&gt; 
            [7] =&gt; 
            [8] =&gt; 
            [9] =&gt; 
        )

    [tmp_name] =&gt; Array
        (
            [0] =&gt; C:\\Windows\\Temp\\php232A.tmp
            [1] =&gt; 
            [2] =&gt; 
            [3] =&gt; 
            [4] =&gt; 
            [5] =&gt; C:\\Windows\\Temp\\php232B.tmp
            [6] =&gt; 
            [7] =&gt; 
            [8] =&gt; 
            [9] =&gt; 
        )

    [error] =&gt; Array
        (
            [0] =&gt; 0
            [1] =&gt; 4
            [2] =&gt; 4
            [3] =&gt; 4
            [4] =&gt; 4
            [5] =&gt; 0
            [6] =&gt; 4
            [7] =&gt; 4
            [8] =&gt; 4
            [9] =&gt; 4
        )

    [size] =&gt; Array
        (
            [0] =&gt; 13781
            [1] =&gt; 0
            [2] =&gt; 0
            [3] =&gt; 0
            [4] =&gt; 0
            [5] =&gt; 75298
            [6] =&gt; 0
            [7] =&gt; 0
            [8] =&gt; 0
            [9] =&gt; 0
        )

)

*/
echo ”;    




  ?&gt;
  
  &lt;!-- Add Images --&gt;
  
  &lt;div class="add-image" &gt; &lt;!-- START FORM DIV --&gt;
  

    
    &lt;form method="post" name="add" action="&lt;?php echo $_SERVER['PHP_SELF']?&gt;?propertyref=&lt;?php echo $_REQUEST['propertyref'];?&gt;" enctype="multipart/form-data"&gt;
      &lt;input type="hidden" name="id" value="&lt;?php echo $_REQUEST['id']?&gt;"&gt;
      &lt;input type="hidden" name="cat_id" value="&lt;?php echo $_REQUEST['cat_id']?&gt;"&gt;
      &lt;fieldset class="add-prop"&gt;
        &lt;legend class="sixteen columns"&gt;Adding images&lt;/legend&gt;

        
        &lt;?php
      for($i=1;$i&lt;=10;$i++):
          echo"&lt;input type='file' class='image-upload' name='foto[]' &gt;\
";      
      endfor;
      ?&gt;
      
        &lt;br /&gt;
        &lt;input type="hidden" name="source" value="&lt;?php echo $source?&gt;"&gt;
        &lt;input type="hidden" name="thumb" value="&lt;?php echo $thumb?&gt;"&gt;
        &lt;input type="submit"  class="image-upload" name="sbmt" value="Image Upload"&gt;
        &lt;br /&gt;
      &lt;/fieldset&gt;
    &lt;/form&gt;
    

  &lt;/div&gt;

What does

adding

$e_arr = $q1->errorInfo();
print_r($e_arr);

after
$q1->execute();

Give you?

Nothing ??

Hi Colin,

try doing something like this from the MySQL prompt:

insert into pics(id) values (somevalue);
select last_insert_id();

and see what is returns when you do this. I suspect that the code is working fine but that it could be a table setup issue.

I ran that in PHP My admin and it works correctly inserting the id and creating a pid. The query works fine using the old coding remember.

Where have you defined $dbh

i.e where is your creation of the base PDO connection e.g

$dbh = new PDO(‘mysql:dbname=my_database;host=127.0.0.1’,$db_username,$db_password);

As an include

    

//PDO CONNECTION

// mysql hostname
$hostname = 'localhost';

// mysql databasename 
$dbname = '****';

// mysql username
$username = '****';

//mysql password
$password = '****';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }


Other pages use this connection with no problem. I wish I had not started this now, working with somebody elses uncommented code is bad enough :slight_smile: