Updating data in database without losing image name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chuckylefrek
    New Member
    • Feb 2007
    • 1

    Updating data in database without losing image name

    I have a page that allows users to update their data. One of the items of data is an image.

    Therefore the update page includes an <input type="file"> field to allow them to choose a different image.

    The problem is, that if the user does not want to change the image and leaves this field blank, when they press the submit button, it updates the database and removes the name of the previous image the user had uploaded.

    There are 5 images in the update form.

    Here is the update code I have at the moment:

    Code:
    $sql = "UPDATE businesslistingtb SET companyname='$companyname', address='$address' , postcode='$postcode' , 
    
    contactname='$contactname' , contactnumber='$contactnumber' , email='$email' , businesstype='$businesstype' , businesshours='$businesshours' 
    
    , whatsunique='$whatsunique' , logo_image='$imagename', testimonial1text='$testimonial1text' , testimonial1who='$testimonial1who' , 
    
    testimonial2text='$testimonial2text' , testimonial3text='$testimonial3text' , products='$products' , services='$services' , awards='$awards' 
    
    , directions='$directions' , paymentmethods='$paymentmethods' , additionalinfo='$additionalinfo' , gallery_image1='$gallery_image1_name' , 
    
    gallery_image2='$gallery_image2_name' , gallery_image3='$gallery_image3_name' , gallery_image4='$gallery_image4_name' WHERE userid=$id";

    Any help much appreciated!

    Paul
  • xwero
    New Member
    • Feb 2007
    • 99

    #2
    You should break up your sql statement and check if an image is uploaded or not. If it is then you add the name to the sql statement

    [PHP]
    $mysql = 'update table set ';
    if(count($_FILE S['image']) > 0){
    $mysql .= 'imagename = "'.$_FILES['image']['name'];
    }
    $mysql .= ' where id = 1';
    [/PHP]

    Comment

    Working...