SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 29
  1. #1
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    PHP Code:

    ################## 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.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    San Sebastian, Spain
    Posts
    93
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I now have:

    PHP Code:

    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.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    San Sebastian, Spain
    Posts
    93
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Probably another type but bindValue? Have you also tested the value of $_REQUEST['id'] to make sure that this is an actual value?

  5. #5
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes $_REQUEST['id'] has a correct value

    What is the problem with bindValue?

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    San Sebastian, Spain
    Posts
    93
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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)?

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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...
    PHP Code:
    $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 ...

  8. #8
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:


    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

  9. #9
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var_dump($dbh); returns: object(PDO)#1 (0) {}

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If you are absolutely sure the new row is being created, then you could try this instead:
    PHP Code:
    $pid PDO::getLastInsertId();
    echo 
    $pid

  11. #11
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK so here is complete code:

    PHP Code:
    <?php
      
    if (isset($_POST['sbmt'])):
        
        
    $cat_id=$_REQUEST['cat_id'];
         
    $propertyref=$_REQUEST['propertyref'];
         
        
                for(
    $i=0;$i<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->prepare($r);
          $q1->bindvalue(1, $file);
          $q1->bindvalue(2, $thumb);
          $q1->execute();
     
    } catch (PDOException $e) {
        print "Error Updating Image!: " . $e->getMessage() . "<br/>";
        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 $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 $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>$target && $height>$target) || $width>$target):
                           
    $cent=100-round((($width-$target)/$width)*100);
                           
    $width=round($width*$cent/100);
                           
    $height=round($height*$cent/100);
                       elseif(
    $height>$target):
                           
    $cent=100-round((($height-$target)/$height)*100);
                           
    $width=round($width*$cent/100);
                           
    $height=round($height*$cent/100);
                       endif;
                       
                       
                       if((
    $width>$target && $height>$target)|| $width>$target):
                            
    $cent=100-round((($width-$target)/$width)*100);
                            
    $width =round($width*$cent/100);
                            
    $height=round($height*$cent/100);
                       elseif(
    $height>$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 $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 $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
    "<script>alert('Image successfully Updated');window.location.href='update-images.php?id=" $_REQUEST['id'] . "&amp;cat_id=" $_REQUEST['cat_id'] . "&amp;propertyref=" $_REQUEST['propertyref'] . "';</script>";        
             endif;
             

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

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

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

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

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

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

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

    )

    */
    echo ;    




      
    ?>
      
      <!-- Add Images -->
      
      <div class="add-image" > <!-- START FORM DIV -->
      

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

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

      </div>
    Appreciate your assistance

  12. #12
    Community Advisor silver trophy

    Join Date
    Nov 2006
    Location
    UK
    Posts
    2,551
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    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);

  13. #13
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    $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

  14. #14
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK so this is where i am at with this and getting rather frustrated with it.

    PHP Code:
    <?php
      
    if (isset($_POST['sbmt'])):
        
        
    $cat_id=$_REQUEST['cat_id'];
         
    $propertyref=$_REQUEST['propertyref'];
         
        
                for(
    $i=0;$i<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->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 ##########
             
      //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->prepare($r2);
          
    $q2->bindParam(":file"$file);
          
    $q2->bindParam(":thumb"$thumb);
          
    $q2->bindParam(":pid"$pid);
          
    $q2->execute();
          
    $count2 $q2->rowCount();
     
    } catch (
    PDOException $e) {
        print 
    "Error Updating Image!: " $e->getMessage() . "<br/>";
        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 $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 $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>$target && $height>$target) || $width>$target):
                           
    $cent=100-round((($width-$target)/$width)*100);
                           
    $width=round($width*$cent/100);
                           
    $height=round($height*$cent/100);
                       elseif(
    $height>$target):
                           
    $cent=100-round((($height-$target)/$height)*100);
                           
    $width=round($width*$cent/100);
                           
    $height=round($height*$cent/100);
                       endif;
                       
                       
                       if((
    $width>$target && $height>$target)|| $width>$target):
                            
    $cent=100-round((($width-$target)/$width)*100);
                            
    $width =round($width*$cent/100);
                            
    $height=round($height*$cent/100);
                       elseif(
    $height>$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 $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 $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
    "<script>alert('Image successfully Updated');window.location.href='update-images.php?id=" $_REQUEST['id'] . "&amp;cat_id=" $_REQUEST['cat_id'] . "&amp;propertyref=" $_REQUEST['propertyref'] . "';</script>";        
             endif;
             

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

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

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

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

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

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

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

    )

    */
    echo ;    




      
    ?>
      
      <!-- Add Images -->
      
      <div class="add-image" > <!-- START FORM DIV -->
      

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

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

      </div>

  15. #15
    Community Advisor silver trophy

    Join Date
    Nov 2006
    Location
    UK
    Posts
    2,551
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    What does

    adding

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

    after
    $q1->execute();

    Give you?

  16. #16
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing ??

  17. #17
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    San Sebastian, Spain
    Posts
    93
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  18. #18
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  19. #19
    Community Advisor silver trophy

    Join Date
    Nov 2006
    Location
    UK
    Posts
    2,551
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    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);

  20. #20
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As an include

    PHP Code:

    //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_ERRMODEPDO::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

  21. #21
    Community Advisor silver trophy

    Join Date
    Nov 2006
    Location
    UK
    Posts
    2,551
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    What does $q1->debugDumpParams();

    give you after executing, it should show the exact SQL and data passed?

  22. #22
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing????

  23. #23
    Community Advisor silver trophy

    Join Date
    Nov 2006
    Location
    UK
    Posts
    2,551
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    Do you have php error reporting switched on? I'd also try throwing a deliberate error with the connection/prepared statement to see what happens, but it looks like there's something amiss with PDO as you're neither getting an error but also not getting any output at all from the debug.

    In these kinds of scenarios I'd create a new test script that is literally a few lines that only processes the most simple PDO transaction you can, with no other dependencies or other points of failure. I'd switch on mysql logging in a staging environment and then see whether anything gets processed at all.

  24. #24
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,276
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    For whatever it's worth, your PDO didn't work on my work computer, but it did work on my home computer. So the code itself is fine. The problem might be other software interfering with the MySQL connection, or a different MySQL version, different PHP version, or different PDO version.
    "First make it work. Then make it better."

  25. #25
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry

    Quote Originally Posted by Jeff Mott View Post
    For whatever it's worth, your PDO didn't work on my work computer, but it did work on my home computer. So the code itself is fine. The problem might be other software interfering with the MySQL connection, or a different MySQL version, different PHP version, or different PDO version.
    I am running this on my local machine using PHP 5.3.6, Apache 2.2.19 and MySQL 5.0.8. The frustrating thing is I have this PDO statement which works 100%

    PHP Code:
        try { 

      
    $r "INSERT INTO `property`(`propertyref`) VALUES (:propertyref)";
       
    $q1 $dbh->prepare($r);
       
    $q1->bindvalue(":propertyref"$_REQUEST['propertyref']);

     
    $q1->execute();
       
      
    $_REQUEST['id']=$dbh->lastInsertId(); // GET LAST id FROM DB


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



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •