insert data with image into MySQL database

Hello friends, today we are going to show you JSON parsing PHP MySQL with image. Accordingly, in the previous tutorial, we did JSON parsing PHP MySQL display data in RecyclerView. This tutorial is the same as the previous tutorial, but different in the image, which means we will upload the image in the PHP MySQL database and retrieve it in RecyclerView.

In the last tutorial, we did the PHP CRUD operation. In this tutorial, we are going to execute how to insert, display, update, delete data in the PHP MySQL database, which means we’ll upload images and store them in the MySQL database.

First, we will insert the data into the PHP MySQL database. So first we need to login web hosting server account or a local computer server like XAMPP or WAMP server. Here we will show data in android RecyclerView so we will need online server.

We will convert PHP MySQL data to JSON format, after that we will display the data in Android RecyclerView using Retrofit and JSON Convert library.

It doesn’t matter if you use a local server like XAMPP or WAMP, because an online free web server is being used to store the database here too. You can also use a free web hosting service for practice to store PHP MySQL database or upload files after completing PHP MySQL tasks in local XAMPP or WAMP server.

What would you do to parse the JSON with the image?

First, we will insert the data along with the image into the PHP MySQL database. We need to create some folders and files to store the image and create the PHP file for the insert operation.

We will create the following file and folder.

student_data (folder name): main folder which we add PHP file and folder in it.

Photo(folder name): for Upload, storing or saving images.

config(file): database connection file.

Index: Main file for inserting data and uploading image.

select: encodes the MySQL data file.

Let’s start understanding JSON parsing with images step by step with example

Creating database and table in PHP MySQL database.

The code of MySQL database and table is given below.

CREATE TABLE `student_data` (
  `id` int(10) NOT NULL,
  `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `image` varchar(100) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

After creating the database, create a folder for storing images in which we’ll upload image. Create a folder for images store.

We need to connect PHP MySQL server to PHP files, so create a connection file to interact database and PHP MySQL file in the file manager.

The following code demonstrates the PHP MySQL connection file.

Config.php

<?php
$server_name='localhost';
$db_user='id18020730_database_username_12345678';
$db_password='1YH]TS4q%h%Jjxhi';
$db_name='id18020730_12345678_dbname';

$conn=mysqli_connect($server_name,$db_user,$db_password,$db_name);
if(!$conn){
    die(mysqli_error());
}
?>

Now we’re making up a new file for inserting data in the PHP MySQL database. For the font-end view. Also, we’ll upload images and stored them in the folder where we already discussed this topic.

The following code demonstrates inserting data with the image in the PHP MySQL database.

Index.php

<?php
require("config.php");
if(isset($_POST['upload'])){
    $name=$_POST['namet'];
    $file_name=$_FILES['file']['name'];
    $tmp_name=$_FILES['file']['tmp_name'];
    
    $query="INSERT INTO student_data(name,image)VALUE('$name','$file_name')";
    $result=mysqli_query($conn,$query);
    
    move_uploaded_file($tmp_name,"photo/".$file_name);
}
mysqli_close($conn);
?>

<html>
    <head>
        <title>
            php mysql android with image
        </title>
    </head>
    <body>
        <form action="index.php" method="POST" enctype="multipart/form-data">
            Name:-<input type="text" name="namet" required/><br >
            file:-<input type="file" name="file" value="upload"/><br >
            <input type="submit" name="upload"/><br >
        </form>
    </body>
</html>

PHP MySQL insert data with image into PHP MySQL database

Insert data into PHP MySQL database is done. We connected the PHP MySQL database and inserted data into the PHP MySQL database along with images. Uploaded the image and stored it in a folder named Photo.

Next we will do JSON parsing with image and display data in android RecyclerView

Note: In this post, if you find any mistakes or bugs and errors, feel free to contact or give feedback to improve the quality. Great if you give your feedback about this tutorial and other tutorials.

Leave a Reply