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:
Select a new image:
Upload the new image:
The image is updated in the mysql table like so:
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?
Here is the current image:
<img src="<?php echo $_SESSION['image']; ?>"></img>
Select a new image:
<input type="file" name="image" value="Select Image">
Upload the new image:
<input type="submit" value="Submit">
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']) . "' ");
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?
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
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Ref. http://www.freshdesignweb.com/10example-ajax-upload-file-with-php.html