Link to home
Start Free TrialLog in
Avatar of razn8
razn8

asked on

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?
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

You may use AJAX way to upload on the clientside process.

Ref. http://www.freshdesignweb.com/10example-ajax-upload-file-with-php.html
Avatar of razn8
razn8

ASKER

Thanks, I'm not using AJAX or Flash for this project. HTML, PHP and MySQL only.
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
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 PHP.net web site in the part about POST-method file uploads.
ASKER CERTIFIED SOLUTION
Avatar of razn8
razn8

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of razn8

ASKER

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.