How to Insert and Display Images in DB by Using PHP and MYSQL


Welcome to MKinventions


hai friends!
In this Artical i am going to show you how to insert Images into Data Base by using PHP and MYSQL.
creating a new data dase and inserting Images is very simple .
now i am  going to create new data base by using the following steps.
here i am going to insert and display Images into data base.


Step 1: index.php                   Download

Step 2: display-image.php    Download    

Step 3: db_connection.php   Download 

Step 4: image_db.php           Download 

Step 5: Create Table in Data Base(SQL file)    Download 


Step 1: In this step we are going to create a new form with title and image and save it as index.php

<html> <head><title>MKinventions</title> <style> .form-control{background-color: aqua; width: 800px;height: 500px;border: 1px solid;border-radius: 10px; box-shadow: 0px 0px 105px 29px grey} .input { margin-top: 18px; height: 40px; width: 350px; border: 1px solid; border-radius: 10px; } .button{background-color: green; width: 100px;height: 40px; color: white; border: 1px solid;border-radius: 10px;} </style> </head> <body> <br><br><br> <center> <div class="form-control"> <h1>MKinventions</h1> <h2>Enter Data</h2><br> <form action="image_db.php" method="post" enctype="multipart/form-data"> <lable>Title</lable> <input type="text" name="title" placeholder="Enter Title" class="input"><br><br> <lable>Upload Image</lable> <input type="file" name="image"><br><br><br> <input type="submit" value="Submit" class="button"> </form> </div> </center> </body> </html>



step 2:Display your Images in display-image.php page.

<html> <head><title>MKinventions</title> <style> .form-control{background-color: aqua; width: 900px;height: 500px;border: 1px solid;border-radius: 10px; box-shadow: 0px 0px 105px 29px grey} .table{width: 790px;border: 1px solid;} </style> </head> <body> <br><br><br> <center> <div class="form-control"> <h1>MKinventions</h1> <h2>Display Data</h2><br> <table cellspacing=10 cellpadding=5 class="table" border="1"> <thead><tr> <th>Sl.no</th> <th>Title</th> <th>Image</th> <th>Date/Time</th> </tr></thead> <tbody> <!--php $i=1; extract($_REQUEST); include("db_connection.php"); $sql=$conn--->query("SELECT * FROM `image_table`"); while($record=$sql->fetch_assoc()) { ?> <tr> <th><!--php echo $record['id'];?--></th> <th><!--php echo $record['title'];?--></th> <th><img height="100" src="images/&lt;?php echo $record[&#39;image&#39;];?&gt;" width="200"/></th> <th><!--php echo $record['CreatedOn'];?--></th> </tr> <!--php $i++; } ?--> </tbody> </table> </div> </center> </body> </html>



step 3:we required this code to connect with data base db_connection.php

<!--php date_default_timezone_set('Asia/Kolkata'); $server="localhost"; $username="root"; $password=""; $dbname="apple"; $conn=new mysqli($server,$username,$password,$dbname); if($conn--->connect_error) { die("connection failed:".$conn->connection_error); } ?>



step 4:we need this code to insert Images into Data Base ,all the image form functionality works in this page ,this will helps to insert Images into DB(image_db.php) page.

<!--php extract($_REQUEST); include('db_connection.php'); move_uploaded_file($_FILES['image']['tmp_name'],"images/". $_FILES['image']['name']); $location=$_FILES['image']['name']; $sql=$conn--->query("INSERT INTO `image_table`(`title`, `image`, `CreatedOn`) VALUES('$title','$location',now())"); if($sql===TRUE) { echo"<script>window.location.assign('index.php?success=success')</script>"; } else { //echo"<script>window.location.assign('index.php?success=failed')</script>"; echo $conn->error; } ?>


step 5: Create Table in Data Base Table name as (image_table) in apple DB

CREATE TABLE `image_table` ( `id` int(20) NOT NULL, `title` varchar(50) NOT NULL, `image` varchar(5000) NOT NULL, `CreatedOn` datetime NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Post a Comment

0 Comments