View Single Post
Old 05-24-2006, 04:41 AM   #1 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Updating an image in mysql DB using PHP

Hi all,

I have a - working - script which uploads new records into a DB with 2 blob images, one for a thumbnail view, the other for larger display.

I re-used this script in another form I have where you are meant to be able to replace a records pictures with new records. Only problem is they are not being uploaded properly, its just showing the little x on the pictures.

The code that works for a new record which I am also using in the other page is as follows:

Note: picture1 and picture2 are the thumbnail and large image browse file field which I get from a file on my computer which is on the server also.

PHP Code:
<?php
//upload images
    //image one
    //code in image path
      
$filepath1='/var/www/html/****/img/';
    
$filepath1 .=$_REQUEST['picture1'];
    
//handle code for first image
    
$isize=filesize($filepath1);
    
$hndl=fopen($filepath1,"rb");
    
//Display error message if no file is uploaded
    
if (!$hndl) {
    echo 
"Cannot open file"; }
    
//Add image data to variable for inserting
    
$imgdata="";
    while(!
feof($hndl)){
    
$imgdata.=fread($hndl,$isize);
    }; 
    
$imgdata=addslashes($imgdata);
    

    
//image two
    //code in image path
     
$filepath2='/var/www/html/****/img/';
    
$filepath2 .=$_REQUEST['picture2'];
    
//hande code for second image
    
$isize2=filesize($filepath2);
    
$hndl2=fopen($filepath2,"rb");
    
//Display error message if no file is uploaded
    
if (!$hndl2) {
    echo 
"Cannot open file"; }
    
//Add image data to variable for inserting
    
$imgdata2="";
    while(!
feof($hndl2)){
    
$imgdata2.=fread($hndl2,$isize2);
    }; 
    
$imgdata2=addslashes($imgdata2);        
 
   
//insert new product details into the product table
   
$sql "UPDATE product SET 
    ProductName = '$name',
    ProductName2 = '$name2',
    ProductCategory = '$category',
    ProductDescription = '$description',
    ProductImageSmall = '"
$imgdata ."',
    ProductImageBig = '"
$imgdata2 ."'
    WHERE ProductID = '$productid'"
;

    
$result = @mysql_query($sql);
    
// error check added
    
if ($result === false) {  die("We're sorry but something unexpected 
has happened..... etc and error no"
); } 
    
    
//Successful upload confirmation message
    
?>

Any suggestions to a change to help the UPDATE process to get the images uploaded correctly is much appreciated!

Note if I update just the information in another second without trying to update the images it works fine, its just the images that arn't being replaced correctly.
BUFFY is offline   Reply With Quote