2

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));
4
  • what is the result, when you output $target_file? Commented May 4, 2018 at 9:12
  • 1
    Your update query has some errors like the opening quote is not closed plus you are also missing a WHERE clause right? Commented May 4, 2018 at 9:15
  • ah sorry yeah forgot to include the where, and the target file just returns "assets/img/products/" Commented May 4, 2018 at 9:24
  • use enctype in "form" tag: <form enctype="multipart/form-data" > Commented May 4, 2018 at 9:59

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

-1

use enctype in form tag

<form enctype="multipart/form-data" >

1 Comment

This should have been a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.