Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 05-24-2006, 03: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
Old 06-04-2006, 04:44 PM   #2 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Just to confirm, if I replace the sql statement with INSERT instead of UPDATE, it inserts an record and the images show fine. But the UPDATE statement only shows an x instead of an image. Im sure the path is being replaced rather then amended. I have tried some error testing but am confused as to why something so similar is not doing the images.

Any suggestions would be great.
BUFFY is offline   Reply With Quote
Old 06-05-2006, 03:40 AM   #3 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 676
DJMaze is on a distinguished road
NEVER EVER USE:
$imgdata2=addslashes($imgdata2);

See http://php.net/mysql_real_escape_string
DJMaze is offline   Reply With Quote
Old 06-05-2006, 07:16 PM   #4 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
K thanks. Any help as to what I could do to make it work though
BUFFY is offline   Reply With Quote
Old 09-02-2006, 12:48 PM   #5 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Sugguestions

Ok so any one have any sugguestions to be able to upload a new picture into an exisiting record, as mentioned earlier I can replace the text but not the images.


Thanks.
BUFFY is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Flash8, PHP and MySQL Redline All Other Coding Languages 2 05-02-2006 10:04 PM
Help with outside connection to mysql using a php page ericanca PHP 11 07-19-2005 10:33 AM
Help with outside connection to mysql using a php page ericanca Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 1 04-18-2005 07:37 PM
Detecting binary image types with PHP . . possible? metazai PHP 6 02-16-2005 10:35 AM
cant connect to mysql databases using php eran PHP 11 08-07-2004 08:02 AM


All times are GMT -8. The time now is 08:51 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting