Saturday, April 23, 2016

Upload Image Using JQuery. Insert Into Database.


How To insert image into Database.
Please follow the below Steps:-

(1) Create a database

   CREATE TABLE IF NOT EXISTS `userinfo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(222) NOT NULL,
`picture` varchar(222) NOT NULL,
`createdDateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

(2) create a folder structure inside your web application folder.

     upload

(3) Create a file with the name index.php

a. Create HTML form
name,userImage
b. load jquery "https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"

c. use id "uploadTopic" for form.

d. Create DIV as follows


e. Place following code at the bottom


4. Create a file upload.php and place the following code:-

mysql_connect("localhost","root","root");
mysql_select_db("test");

if(!empty($_POST))
{
  $name = $_POST['name'];
  if(isset($_FILES['userImage']['name']) && $_FILES['userImage']['name']!='')
  {
 $desired_path = 'upload/';

 $rand=rand(0,999);
 $desired_file_name = $rand.'_'.str_replace(' ','_',strtolower($_FILES['userImage']['name']));

 move_uploaded_file($_FILES['userImage']['tmp_name'], $desired_path . $desired_file_name);
  }

$query_insert="INSERT INTO userInfo SET name = '".$name."',picture='".$desired_file_name."'";
mysql_query($query_insert);
}
?>