Reputation: 35
I am able to upload an image file path to the database, although when trying to update it, the file name show as blank on the db.
<input type='file' id='image' name='image'>
$target_dir = "assets/img/products/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);
$img = mysqli_real_escape_string($conn, $target_file)
$sql = "UPDATE products SET image = '$img' WHERE product_ID='$index'";
mysqli_query($conn,$sql) or die(mysqli_error($conn));
Upvotes: 2
Views: 87
Reputation: 51
use enctype in form tag
<form enctype="multipart/form-data" >
Upvotes: -1
Reputation: 5048
You are probably reposting the same form, and the input [name="image"] is empty, so you update the field with an empty value
You should check if you have any file posted before performing the update on the image field:
if(file_exists($_FILES['iamge']['tmp_name']) && is_uploaded_file($_FILES['image']['tmp_name'])) {
// perform update
}
Upvotes: 2