Almeister9 Almeister9 - 1 month ago 9
PHP Question

php upload file on Edit form, prevent UPDATE if no file

I have a form to Edit existing values in a MySQL table.
One of the fields stores the filename of an image, (another stores the folder location of the image)
The file has an upload field to upload another image and replace the image name in the original image name field.

This works fine if I choose a file, to replace the existing image.
If I don't choose to replace the image, the field is UPDATE with the empty data.

Basically I am trying to only do the UPDATE sql query if there is an image chosen and uploaded.

I am using this code:

if(!empty($_FILES['file_slick']))
{
$slick = $_FILES['file_slick']['name'];
copy($_FILES['file_slick']['tmp_name'],'../images/product/'.$folder.'/'.$slick);
$sql_slick = "UPDATE tbl_product SET prod_slick = '".$slick."' WHERE prod_id = '".$prodid."'";
mysql_query($sql_slick);
}


I have tried if(!empty($_POST['file_slick'])) but neither works. If a file is chosen and uploaded, it is moved to the correct folder and the image filename is updated into the field. If an image is not chosen, empty data is updated into the image filename field.

I have looked around the internet (and mostly here) and from what I can see, my check to prevent empty data should work.

Can anyone see anything wrong here?

Answer

try if(!empty($_FILES['file_slick']['name']))

Comments