Ask Question

Menu

How to Insert image In MySQL Using PHP

For insert image in MySQL first we have to create a table in data base.

CREATE TABLE `image` (
`image` LONGBLOB NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Here we using 1 file for insert image in MySQL:

<!DOCTYPE html>
<html>
<head>
<title>Insert Image in MySql using PHP</title>
</head>
<body>
<?php
$msg = '';
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_FILES['image']['tmp_name'];
$img = file_get_contents($image);
$con = mysqli_connect('127.0.0.1:3306','root','','admin') or die('Unable To connect');
$sql = "insert into image (image) values(?)";
$stmt = mysqli_prepare($con,$sql);
mysqli_stmt_bind_param($stmt, "s",$img);
mysqli_stmt_execute($stmt);
$check = mysqli_stmt_affected_rows($stmt);
if($check==1){
$msg = 'Image Successfullly UPloaded';
}else{
$msg = 'Error uploading image';
}
mysqli_close($con);
}
?>
<form action="insert.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<button>Upload</button>
</form>
<?php
echo $msg;
?>
</body>
</html>






Subscribe with us to get latest topic update






Choose a Language



Subscribe