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>
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?