I am trying to update an image in MySQL database but each time I do try to update, other fields (i.e. location) of the is updated but the image name becomes blank. Below is the code for updating:
if (isset($_POST['edt_Pic'])) {
$id = mysqli_real_escape_string($con, $_POST["id"]);
$file = mysqli_real_escape_string($con, $_POST['file']);
$fullname = mysqli_real_escape_string($con, $_POST['fullname']);
$filename = $_FILES['file']['name'];
$file_temp = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
if ($file_size > 1048576) {
echo "<script type='text/javascript'>alert('Attached photo exceeds 1Mb. Kindly reduce the size.');
</script>";
echo "<script>window.location.href='users.php'</script>";
} elseif (!empty($_FILES["file"]["tmp_name"])) {
$exp = explode(".", $file_name);
$ext = end($exp);
$name = $_FILES['file']['name'];
move_uploaded_file($_FILES["file"]["tmp_name"], "./users/" . $name);
$location = "./users/" . $name;
$sql = "UPDATE users
SET file = ?, location = ?, fullname = ?
WHERE id = ?";
$stmt = mysqli_prepare($con, $sql);
mysqli_stmt_bind_param($stmt, "sssi", $file, $location, $fullname, $id);
if (mysqli_stmt_execute($stmt)) {
echo "<script type='text/javascript'>alert('Image for " . $fullname . " has updated successfully.');
</script>";
echo "<script>window.location.href='users.php'</script>";
} else {
echo "<script type='text/javascript'>alert('Oops! Something went wrong. Please try again.');
</script>";
echo "<script>window.location.href='users.php'</script>";
}
}
mysqli_stmt_close($stmt);
}
mysqli_close($con);
I have tried to add the old file name input field but to no luck.
$fullname
? Well then presumably$_POST['fullname']
was either not set, or contained only an empty string.mysqli_real_escape_string
in combination with prepared statements, makes no sense. It will falsify the data you insert, when it contains special characters.file
is atype="file"
input field, then this will cause only$_FILES['file']
to be populated,$_POST['file']
won't even be set then.