I try to upload image in my database. I write these code, but it's not update may database.

<?php
// Include the database configuration file

$msg = "";

// If upload button is clicked ...
if (isset($_POST['upload'])) {

    $filename = $_FILES["uploadfile"]["name"];
    $tempname = $_FILES["uploadfile"]["tmp_name"];
    $folder = "./image/" . $filename;

    $db = mysqli_connect("localhost", "root", "", "madrasadb");

    // Get all the submitted data from the form
    $sql = "INSERT INTO `smash` (`stuimage`) VALUES ('$filename')";

    // Execute query
    mysqli_query($db, $sql);

    // Now let's move the uploaded image into the folder: image
    if (move_uploaded_file($tempname, $folder)) {
        echo "<img src=" . $folder . " height=200 width=300 />";
    } else {
        echo "<h3> Failed to upload image!</h3>";
    }
}
?>



<!DOCTYPE html>
<html>

<head>
    <title>Image Upload</title>
</head>

<body>
    <div id="content">
        <form method="POST" action="" enctype="multipart/form-data">
            Select Image File to Upload:
            <input type="file" name="file">
            <input type="submit" name="submit" value="Upload">
        </form>
    </div>

</body>

</html>

Dani AI

Generated

Quick summary: the immediate cause was a mismatch between the form field names and the server-side checks — spotted that and confirmed the upload worked after the fix. The rest below focuses on robust, production-ready checks and debugging steps to prevent silent failures when saving uploads to disk and inserting paths into a database.

Checklist and quick fixes

  • Confirm the HTML form actually sends a file (POST with multipart/form-data) and the server-side code reads the same field name the form uses.
  • Inspect the server-side upload metadata before doing anything (check the upload status and file size). That often reveals permission or client-side problems.
  • Verify the target directory exists and is writable by the webserver process. Missing or non-writable folders are a very common cause of move failures.
  • Make filenames safe and unique before saving to disk to avoid collisions and directory traversal issues.
  • Prefer to store the file path in the database rather than raw binary blobs unless you have a clear reason to store blobs.

Security and reliability

  • Validate uploads with the fileinfo extension (server-side MIME check) and restrict allowed image types and maximum sizes.
  • Use prepared statements or parameterized queries for any DB insert to avoid injection and to reliably detect DB errors.
  • Always check and log the actual error messages from both the filesystem operation and the database layer so failures are visible.

Debugging tips and references

Recommended Answers

All 2 Replies

First replace
if (isset($_POST['upload']))
with
if (isset($_POST['file']))
Second replace
$filename = $_FILES["uploadfile"]["name"];
with
$filename = $_FILES["file"]["name"];
Also replace
if (isset($_POST['upload']))
with
if (isset($_POST['submit']))

To check that file was inserted into database
you can replace

// Execute query
    mysqli_query($db, $sql);

with

// Execute query
        mysqli_query($db, $sql);
        printf("<br>Affected rows (INSERT): %d\n", mysqli_affected_rows($db));

Thanks a lot. The problem was solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.