Upload Image In PHP MySQL Database And Display
Last Updated : Mar 11, 2024
IN - PHP MySQL | Written & Updated By - Amruta
In this tutorial we will show you the solution of upload image in PHP MySQL database and display and we are going to understand how to upload an image in PHP using the MySQL database and how to display it.
There are many ways to help of which we can upload an image using PHP.
In this tutorial, we are going to understand one of them. Let us see the example below to see how we are going to use it.
Step By Step Guide On Upload Image In PHP MySQL Database And Display :-
Now, first of all, upload and display an image with help of a database using PHP.
We must have a database and table created with a PHP form. PHP forms help us to upload an image whereas a database is used to store and display it.
So, let us see the codes given below to see how to do this with help of codes.
<?php if (isset($_POST['upload_image'])) { $filename = $_FILES["image_upload"]["name"]; $tempname = $_FILES["image_upload"]["tmp_name"]; $folder = "./image_folder/" . $filename; $connect = mysqli_connect("localhost", "root", "", "database_name"); $query = "INSERT INTO table_name (filename) VALUES ('$filename')"; mysqli_query($connect, $query); if (move_uploaded_file($tempname, $folder)) { echo "<h2> Image uploaded successfully! </h2>"; } else { echo "<h2> There is some error! </h2>"; } } ?> <!DOCTYPE html> <html> <head> <title> Upload image in PHP MySQL database and display </title> </head> <body> <h1> TalkersCode </h1> <h2> Upload image in PHP MySQL database and display </h2> <div id="container"> <form method="POST" action="" enctype="multipart/form-data"> <input class="form-control" type="file" name="image_upload" value="" /> <button class="btn btn-primary" type="submit" name="upload_image">UPLOAD IMAGE</button> </form> <?php $query = " select * from table_name "; $result = mysqli_query($connect, $query); while ($result = mysqli_fetch_assoc($result)) { ?> <img src="./image_folder/<?php echo $result ['imagename_here']; ?>"> <?php } ?> </div> </body> </html>
- As, here we see that in the above example, we show you how HTML and PHP codes are used.
- Here, first of all, we create a basic structure of HTML, in which we use <!DOCTYPE html> which defines the type of document. And next one is our HTML tags. These tags are paired tags and all the data regarding HTML is written inside these tags.
- After we use our head tag which is again paired tag and contains the title and meta information of the webpage. The data written inside the head is not shown on the webpage.
- Now, next is our title tag which defines the title of the webpage. The tag which has its closing tag is known as a paired tag. So, this is again a paired tag.
- Now, next is the body which is the main tag of HTML. The data which we have written inside the body is shown on the webpage. Mostly all tags which are helpful to show data or information on the screen are written under the body tag.
- As we see in the body we create a field of input type file to upload an image and a button to submit that image. Now, the rest is done with help of PHP codes. Let us understand our PHP codes
- Inside PHP codes we create a connection with a database and a table with some required fields. When submit button is clicked PHP codes come into action and the image gets uploaded.
- Two main lines here to note are move_uploaded_file() and enctype = multipart/form-data. There are mandatory to upload an image from the form and to store the image in a folder that is uploaded.
- At last, we MySQL queries to fetch images and display all the images with help of while loops. This all is done with help of a database connection.
Conclusion :-
At last, in conclusion, here we can say that with the help of this article we can understand how to upload an image in the PHP MySQL database and display it.
I hope this tutorial on upload image in PHP MySQL database and display helps you and the steps and method mentioned above are easy to follow and implement.