How To Upload and Display Image PHP HTML SQL
How To Upload and Display Image PHP HTML SQL
Here we have html form for selecting image from the computer.
The image is uploaded to a folder and its name stored in a mysql database as a text.
1 <?php
2 $conn = mysqli_connect("localhost", "root", "");
3 if ($conn) {
4 echo "<br />connected to server......";
5 } else {
6 die("Failed to connect ". mysqli_connect_error());
7 }
8
9 $selectalreadycreateddatabase = mysqli_select_db($conn, "uploaddisplay")
10 if ($selectalreadycreateddatabase) {
11 echo "<br /> Existing database selected successfully";
12 } else {
13 echo "<br /> Selected Database Not Found";
14 $createNewDb = "CREATE DATABASE IF NOT EXISTS `uploaddisplay`";
15 if (mysqli_query($conn, $createNewDb)) {
16 echo "<br />New Database Created Successfullly";
17 $selectCreatedDatabase = mysqli_select_db($conn, "uploaddisplay");
18 if ($selectCreatedDatabase) {
19 echo "<br />Created Database Selected Successfullly";
20 // Creating new table
21 $sqlcreatetable = "
22 CREATE TABLE IF NOT EXISTS `updis` (
23 `id` int(11) NOT NULL AUTO_INCREMENT,
24 `name` varchar(100) NOT NULL,
25 PRIMARY KEY (`id`)
26 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
27 ";
28
29 if (mysqli_query($conn, $sqlcreatetable)) {
30 echo "<br />New table Created";
31 } else {
32 echo "<br /> Unable to create new table.";
33 }
34
35 }
36 } else {
37 echo "<br />Unable to create database";
38
39 }
40 }
41
42
43 if(isset($_POST['fileuploadsubmit'])) {
44 $fileupload = $_FILES['fileupload']['name'];
45 $fileuploadTMP = $_FILES['fileupload']['tmp_name'];
46 $folder = "images/";
47 move_uploaded_file($fileuploadTMP, $folder.$fileupload);
48 $sql = "INSERT INTO `updis`(`name`) VALUES ('$fileupload')";
49 $qry = mysqli_query($conn, $sql);
50 if ($qry) {
51 echo "<br />uploaded";
52 }
53 }
54
55 $select = " SELECT * FROM `updis` " ;
56 $query = mysqli_query($conn, $select) ;
57 while($row = mysqli_fetch_array($query)) {
58 $image = $row['name'];
59 echo '<img src="images/'.$image.'" height="150" width="150" >';
60 }
61
62 if (mysqli_close($conn)) {
63 echo "<br />Connection closed.........";
64 }
65 ?>
66 <!DOCTYPE html>
67 <html>
68 <body>
69 <form method="post" action="" enctype="multipart/form-data">
70 <input type="file" name="fileupload" />
71 <input type="submit" name="fileuploadsubmit" />
72 </form>
73 </body>
74 </html>
References:
http://mauricemutetingundi.blogs...
23 Views
How can we open GitHub projects on IDEs like NetBeans and Eclipse?
238 Views
Which is the best way to store images in server using MySQL BLOB or using normal PHP
upload script?
16 Views