How to insert image in phpmyadmin
Uploading the image/videos into the database and displaying it using PHP is the way of uploading the image into the database and fetching it from the database. Using the PHP code, the user uploads the image or videos they are safely getting entry into the database and the images should be saved into a particular location by fetching these images from the database.
Approach: Make sure you have XAMPP or WAMP server installed on your machine. In this tutorial, we will be using the WAMP server.
1. Create Database: First, we will create a database named ‘geeksforgeeks‘. You can use your existing database or create a new one.
create database “geeksforgeeks”
2. Create Table: Create a table named ‘image‘. The table contains two fields:
The id should be in Auto incremented(AI). Your table structure should look like this:
table structure of “image”
Or you can create a table by copying and pasting the following code into the SQL panel of your PHPMyAdmin.
CREATE TABLE IF NOT EXISTS `image` ( `id` int(11) NOT NULL AUTO_INCREMENT, `filename` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
To do this from SQL panel refer to the following screenshot.
create a table ‘image” from the SQL panel
We will be using Bootstrap here to use Bootstrap’s form control. Below is the code to include the Bootstrap CDN link in the head section of the HTML code.
Creating folder and files:
We will now create a folder named “image“. The files uploaded by the client on the server will be stored in this folder. Create index.php and style.css. Keep your main project folder (for example here.. GeeksForGeeks) in the “C://wamp64/www/“, if you are using WAMP or “C://xampp/htdocs/” folder if you are using the XAMPP server respectively. The folder structure should look like this:
Program: Now, we will create an HTML form for uploading image files (you can upload any type of file like .pdf or .mp4) and will display the uploaded image.
Explanation of PHP code:
You can copy the above code and mention it into the main code directly or create a link as same in the HTML code and attach it with the main code which is given below. As mentioned that if you link the stylesheet file you should create another file in .css format and save it in the place where the main file is to be saved. The form
created with the help of the POST method and the enctype=”multipart/form-data”is the action which encodes the files and allows you to send them through POST.
Explanation: The following are the explanation to create the PHP code which is the following:
Combination of the above codes: The final code of upload the image into MySQL using PHP is as followed.
Conclusion: The uploaded image into the database with the PHP code is simple and used for various purposes. The code helps to upload the image and then uploaded the image into the database and can be shown in another folder.
upload_max_size = 100M post_max_filesize = 100M
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Can we insert image in MySQL?
You can try the below code for inserting an image: INSERT INTO xx_BLOB(ID,IMAGE) VALUES(1,LOAD_FILE('E:/Images/jack. jpg'));
Which datatype is used for image in phpMyAdmin?
In MySQL, the preferred data type for image storage is BLOB.
Can we add image in database?
Well, you can put it into a database (with modern versions of mySql), but it's better to upload the file to either your server, or someone elses (like imgur) and store the URL in the database.
Can I store image in database?
To insert images into a database, the database must support images. Images are stored in binary in a table cell. The data type for the cell is a binary large object (BLOB), which is a new SQL type in SQL3 for storing binary data.