file input update mysql when selected

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Loganathan NatarajanLAMP DeveloperCommented:
You may use AJAX way to upload on the clientside process.

razn8Author Commented:
Thanks, I'm not using AJAX or Flash for this project. HTML, PHP and MySQL only.
Loganathan NatarajanLAMP DeveloperCommented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Ray PaseurCommented:
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.
razn8Author Commented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
razn8Author Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.