Saed Q. Yousef Saed Q. Yousef - 12 days ago 8
MySQL Question

Issue in uploading images to MySQL database using PHP


I have this php code to upload image to the database , I have issue
with it and I don't know what is it , the database table name is
images > and the fields are id , name VARCHAR() , photo LONGBLOB


<?php
ini_set('display_errors', '1');
$servername = "";
$username = "";
$password = "";
//$host = "";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"/>
</br>
</br>
</br>
<input type="submit" name="go" value="Upload"/>
</form>
<?php
if(isset($_POST['go'])){
if(getimagesize($_FILES['image']['tmp_name']) == FALSE){
echo "Select a photo please";
}else {
$image = addslashes($_FILES['image']['tmp_name']);
$name = addslashes($_FILES['image']['name']);
$image = file_get_contents($image);
$image = base64_encode($image);
save_image($image , $name);
}
}
function save_image($image , $name){
$servername = "localhost";
$username = "cl60-shooters";
$password = "dbsjcNs-b";
$conn = new mysqli($servername, $username, $password);
$qry = "insert into images (photo , name) VALUES ('$image','$name')";
$result = mysqli_query($conn,$qry);

if($result){
echo "Successfull upload";
}else{
echo "try Again";
print_r($result);
}



}

?>
</body>
</html>



the result is as shown in the attached screenshotResult

Answer

You're not using database name in the mysqli constructor. It should be like the following:

$servername = "localhost";
$username = "cl60-shooters";
$password = "dbsjcNs-b";
$database = "database_name_here";
$conn = new mysqli($servername, $username, $password, $database);

Hope it should work now.