Upload Image into Mysql Database - Php

How to upload image into mysql database using php code?

Snippet Code

Rate this page :
  [ 0 votes]

Use this simple php code to upload image to mysql database. You have to set image column type as "blob" for your table.

CREATE TABLE IF NOT EXISTS `images` ( `id` int(11) NOT NULL AUTO_INCREMENT, `image` blob NOT NULL, PRIMARY KEY (`id`), KEY `id` (`id`) ) <?php error_reporting(0); $conn = mysql_connect("localhost","root",""); $dbcon = mysql_select_db("testdb"); if(isset($_POST['sbmit'])) { $image = addslashes(file_get_contents($_FILES['img']['tmp_name'])); //you keep your column name setting for insertion. I keep image type Blob. $query = "INSERT INTO images (id,image) VALUES('','$image')"; $qry = mysql_query($query); if($qry) { $msg='Inserted Successfully'; } } ?> <form name='imgdb' method="post" action="" enctype="multipart/form-data"> <input type='file' name='img'> <input type='submit' value='submit' name='sbmit'> <div><?php echo $msg; ?></div> </form>


Ask Questions

Ask Question