file input update mysql when selected

Posted on 2011-10-10
Last Modified: 2012-05-12
I am selecting an image filename from a mysql table and displaying the image on the page, as well as displaying a file input form field where the displayed image can be updated with a new image, like so:

Here is the current image:
<img src="<?php  echo $_SESSION['image']; ?>"></img>

Open in new window

Select a new image:
 <input type="file" name="image" value="Select Image">

Open in new window

Upload the new image:
 <input type="submit" value="Submit"> 

Open in new window

The image is updated in the mysql table like so:

 $target = "images/"; 
 $target = $target . basename( $_FILES['image']['name']); 

 $picture = htmlentities(($_FILES['image']['name'])); 

mysql_query("UPDATE users SET image = '" . mysql_real_escape_string($image) . "'
WHERE id = '" . mysql_real_escape_string($_SESSION['userid']) . "' ");

Open in new window

What I want to achieve is: if a file input is selected, then the image field is updated in the mysql table. If no file input is selected, then the mysql field is not updated, leaving the current image intact.

What is the best way to do this?
Question by:razn8
    LVL 36

    Expert Comment

    by:Loganathan Natarajan
    You may use AJAX way to upload on the clientside process.


    Author Comment

    Thanks, I'm not using AJAX or Flash for this project. HTML, PHP and MySQL only.
    LVL 36

    Expert Comment

    by:Loganathan Natarajan
    What I want to achieve is: if a file input is selected, then the image field is updated in the mysql table

    What do you mean, you need to handle as event OR just click the button after input field selected ??

    You can do this with AJAX
    LVL 107

    Expert Comment

    by:Ray Paseur
    Is there a little fifty-point question here that we can answer, or is this a need for application development?

    You can test the $_FILES array to see if an image was uploaded.  See the web site in the part about POST-method file uploads.

    Accepted Solution

    I appreciate your responses. Neither answers helped me, so I figured it out on my own, as described below:

    I am loading a user image from the database via a session. Then, below the displayed image is a file upload form from which the user can update the image currently stored in the database. There are also other form fields that will be updated along with the image.

    <form action"confirm.php">
    Edit your name: <input name="name" type"text" value="<?php  echo $_SESSION['name']; ?>"><br/>
    Current Picture: <img src="<?php  echo $_SESSION['picture']; ?>"><br/>
    Update Picture: <input type="file"  name="newpicture" /><br/>
    <input name="Submit" type="submit" value="Submit">

    Open in new window

    On confirm.php I display the data and image so the user can confirm prior to updating.
    If a new image was uploaded, then '$newpicture' displays. If a new image was not uploaded then '$_SESSION['picture']' displays.
    The database image entry has the full url, however the updloaded image has a relative url. So I define the full url with $pictureurl variable. Whichever image is displayed needs to be updated in the database upon the confirmation submit. I define this with the $updatepicture variable.

    Form confirmation page:
    $uploaddir = "pictures/";
    $path = $uploaddir.$HTTP_POST_FILES['newpicture']['name'];
    $picturedir = 'http://finalimage.url/pictures/';
    echo $name;
    if($newpicture != none){ 
        if(copy($HTTP_POST_FILES['newpicture']['tmp_name'], $path)){ 
            $theFileName = $HTTP_POST_FILES['newpicture']['name'];
    $pictureurl = $picturedir.$theFileName;
    echo "<img width=200 src='".$pictureurl ."'>";
    $updatepicture = $pictureurl;
        } else {
    echo "<img width=200 src='".$_SESSION['picture'] ."'>";
    $updatepicture = $_SESSION['picture'];
    <form action"update.php">
    <input name="name" type="hidden" value="<?php echo $name ?>">
    <input name="updatepicture" type="hidden" value="<?php echo $updatepicture ?>">
    <input name="Submit" type="submit" value="Submit">

    Open in new window

    The update.php page is a basic mysql update:
    mysql_query("UPDATE users SET fieldname = '$_POST['formfield']'
    WHERE id = $loginid ");

    Open in new window

    I posted this here so that someone else with the same question can see how it was solved. Feel free to add your comments to improve my solution. I removed file cleansing to reduce the amount of code displayed here, so add that back in before using this code live.

    Author Closing Comment

    I appreciate both logudotcom and Ray_Paseur taking the time to post comments. However, their answers did not provide the specific information I needed to solve this question. Therefore, I spent the time to figure it out on my own.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now