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>
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.