user6905628
user6905628

Reputation:

View the last image uploaded to the database using php

I am new in php and I am trying to view the last image uploaded to the database using php. insertion code running perfectly but the selection one accrued an issue.

here is my code

<?php
if (isset($_POST['UploadImage'])) {
$image = $_FILES['image']['tmp_name'];
$image_type = $_FILES['image']['type'];
if ($image == '') {
    echo "<script> alert('please choose picture') </script>";
} else if ($image_type != "image/jpeg" && $image_type != "image/png" && $image_type != "image/gif") {
    echo "<script> alert('image format is not correct ') </script>";
} else {

    $query = "INSERT INTO  image (ImageLink) VALUES ('" . $image . "')";
    if (mysqli_query($db1, $query)) {
        echo '<script> alert("image uploaded successfully") </script>';
    } else {
        echo '<script> alert("please try again") </script>';
    }
}

if (mysqli_affected_rows($db1) > 0) {
    $query_image = "SELECT ImageLink FROM image ORDER BY Image_Id DESC LIMIT 1;";
    $result = mysqli_query($db1, $query_image);
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<img width='450' height='600' src='data:image/jpeg; base64,".base64_encode($row['ImageLink'])."' >";
        echo "<br>";
    }
}
}?>

while running... image doesn't appear in page. please any help in doing that... thank you

Upvotes: 1

Views: 54

Answers (1)

user6905628
user6905628

Reputation:

Solution is:
HTML code

<table
            style="border-style: solid; border-color: gray; margin-top: 17px; width: 100%; height: 50%; width: 100%">
            <tr>
                <td>please upload a picture :</td>
                <td><input type="file" name="image" id="image"></td>

            </tr>
            <tr></tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="Upload Image" name="UploadImage"
                    id="UploadImage"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
            </tr>
        </table>

PHP Code

<?php
if(isset($_POST["UploadImage"])){
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["image"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

    // Check if image file is a actual image or fake image
    if (isset($_POST["UploadImage"])) {
        $check = getimagesize($_FILES["image"]["tmp_name"]);
        if ($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }
    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["image"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
        // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
            echo "The file " . basename($_FILES["image"]["name"]) . " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }

        $query = "INSERT INTO  image (ImageLink) VALUES ('" . $target_file . "')";
        if (mysqli_query($db1, $query)) {
            echo '<script> alert("Image Has been Uploaded Successfully") </script>';
        } else {
            echo '<script> alert("Please Try Again") </script>';
        }

        if (mysqli_affected_rows($db1) > 0) {
            $query_image = "SELECT ImageLink FROM image ORDER BY Image_Id DESC LIMIT 1;";
            $result = mysqli_query($db1, $query_image);
            while ($row = mysqli_fetch_assoc($result)) {
                // var_dump( $row );
                echo "<img width='450' height='600' src='data:image/jpeg; base64,".base64_encode(file_get_contents($row['ImageLink']))."'>";
                echo "<br>";
            }
        }
    }
}

Upvotes: 1

Related Questions