0

I am trying to input image into a localhost database, but my code refuses to cooperate. The "database[2].php" part is a PDO connection. The values of the database are id, image, and name and the database is called image.

<form action="image.php" method="post" enctype="multipart/form-data">
    <input type="file" name="image" class="inputBox">
    <input type="submit" class="inputBox"  value="go.">
</form>
<?php

include 'database[2].php';


if(isset($_POST['submit'])){
    if(getimagesize($_FILES['image']['tmp_name']) === FALSE){
        echo "<div class='titleBox'>Please select an image.</div>";
    } else {
        $image = addslashes($_FILES['image']['tmp_name']);
        $name = addslashes($_FILES['image']['tmp_name']);
        $image = file_get_contents($image);
        $image = base64_encode($image);
        saveImage($name, $image);
    }
}

function saveImage($name, $image) {

    $sql = "INSERT INTO image (name, image) VALUES (:name, :image)";
    $stmt = $conn->prepare($sql);

    $stmt->bindParam(':image', $_POST['image']);
    $stmt->bindParam(':name', $_POST['name']);

    if($stmt->execute()):
        echo "<div class='titleBox'>Image uploaded.</div>";
    else:
        echo "<div class='titleBox'>Image not uploaded.</div>";
    endif;

}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

0 Answers0