Insert Data In MySql Database Using PHP 7.2 

In this article we are going to share with you how to insert data in MySQL database using PHP. Most people have a little bit confused about how they can insert data in MySQL database using PHP 7.2. This is the current latest version PHP. In this article, we gonna talk about how in a Mysql database using object-oriented programming. the object-oriented programming is just the class and object that gives you to create properties and methods. There is a different way to insert data using PHP but this is the most usable way because you can access this class anywhere if you need to install data in your Mysql database.

Now the most important part of this tutorial is making the class. You can use this class to build a complex code for your web application. In this tutorial, I just wanted to share the basic concept behind the class. Using the class you can insert update delete and select data from the Mysql server. So if you just need to understand what is the object-oriented programming then you can just skip this article and read my previous articles that give you the complete understanding of object-oriented programming.

Using object-oriented programming you can achieve the most valuable things and you can do the hard thing with the simple aspect. Now just move on to the article main topic and Let’s create an example that shows you how to insert data in Mysql database using PHP 7.2.


Create a simple file called sample.php

Now let’s create a simple file called sample.php. In this file we will just put a class and insert properties and methods.  we create a method to insert data in the MySQL database. To get the complete understanding of how to connect your database to MySql database we have one more article that give you and complete understanding about that. If you don’t know how to connect your PHP to MySQL database then this article will help you to do that. Now just look at the example code.

According to the code, we just create a connection using the constructor and create a method that inserts the value into your database. We also have some properties that initialize the connection.  If you have a different username password or the server then you can change these properties also. In the insert_data() function We just want to specify some parameter.

When we call this method we pass an argument to this method. The argument is just a value pass to the method that can be manipulated and returned by the function. In some situation, it might be not returned by the function. so in the insert method, we just create the SQL Query.  The SQL Query is just the SQL statement that executes an insert command. If you don’t know what is insert command then you just need to read the basic command in Mysql server.

Query() Method

Then we just execute the query using a query method and past the SQL statement to this query method. We will check if the query method is returning true then we send the successful message to the user otherwise we return the error message as simple as that.  The query method will check the connection and the SQL statement is in the correct format and also check the connection property is the valid connection. If both return true then the insert statement will execute.

if you have an error in connection properties or in the SQL statement then it might be send the error message to the user.


Accessing the Class Method

Now the simple thing is that you can now access this class and call the method using some parameters.  keep in mind that when you call the method you have to specify the exact parameter to the method. if you specify incorrect parameter then it would be an error message sent by the PHP server.

Now you can call the class method using the instance of the class. We call the method and specify values for the method. Keep in mind that we put this above code in the separate file. So if you place this code in any of the PHP files accept the class you can access the class and the methods and properties of that class and also remember you specify required keyword on the top and specify the class file in the required statement.

Now you are able to insert data in Mysql database using PHP. Now you know that how to insert data in the MySQL server. On this website, you can find more about inserting and retrieving data from the MySQL server so keep reading articles on this website.

So that is all for now.  we will see you in the next tutorial