Saed Q. Yousef Saed Q. Yousef - 1 year ago 76
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

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";
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" name="go" value="Upload"/>
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);

echo "Successfull upload";
echo "try Again";



the result is as shown in the attached screenshotResult

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download