-4

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.

6
  • You're talking about $fullname? Well then presumably $_POST['fullname'] was either not set, or contained only an empty string.
    – C3roe
    Commented 2 days ago
  • 4
    FYI: Using mysqli_real_escape_string in combination with prepared statements, makes no sense. It will falsify the data you insert, when it contains special characters.
    – C3roe
    Commented 2 days ago
  • @C3roe I am talking about $file. The code is unable to post the file name to the database by making the database cell empty. For $location and $fullname the code is working.
    – Speed Ack
    Commented 2 days ago
  • 1
    Well if file is a type="file" input field, then this will cause only $_FILES['file'] to be populated, $_POST['file'] won't even be set then.
    – C3roe
    Commented 2 days ago
  • 1
    This is a good time for you to start doing some debugging. When you debug the code shown, which specific value or result of a specific operation isn't what you expect? Where did the incorrect value come from?
    – David
    Commented 2 days ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.