How to upload a file chosen by user and insert it into a database

2020-02-15 php mysql file-upload

I have been trying to insert an image to the database but am failing. I am geting an error that Undefined index : image, all other information i can insert but image, is failing to insert below is the code please help.

<?php
$user = 'root';
$password = ''; //To be completed if you have set a password to root
$database = 'students'; //To be completed to connect to a database. The database must exist.
$port = NULL; //Default must be NULL to use default port
$mysqli = new mysqli('127.0.0.1:3308', $user, $password, $database, $port);

if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}else{


    $image = file_get_contents($_FILES["image"]["tmp_name"]);
    $query = "INSERT INTO `image` (`image`) VALUES('$image')";
    if(mysqli_query($mysqli,$query)){
        echo "succesfull";

    }else{
        echo "undone";
    }
}





?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>image upload</title>
</head>
<body>
   <form method="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="submit">
</form>
</body>
</html>

Answers

Related