All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Insert Data In MySQL Using PHP

Last Updated : Mar 11, 2024

How To Insert Data In MySQL Using PHP

In this tutorial we will show you the solution of how to insert data in MySQL using PHP, while developing web pages, to store and retrieve data, we use MySQL database.

We can easily store data in PHP based web pages, by using basic commands of MySQL within the code.

In this tutorial, we’re going to learn that how we can insert data in MySQL by using PHP.

Step By Step Guide On How To Insert Data In MySQL Using PHP :-

MySQL -

MySQL is a database management system which is use to add, access, and process data stored in a computer database. In order to use this, you need a database management system such as MySQL Server in your system.

Requirements -

  • XAMPP (Web Server)
  • Installed PHP
  • Installed MySQL
  • Sublime Text Editor

For inserting data in MySQL, the PHP file have to get connected to the database and table which has been already created in the MySQL databases.

For insert data in MySQL first we have to create a table in data base.

Here we using 3 file for insert data in MySQL -

1. DBCON.php - For connecting data base to the frontend PHP file.

2. insert.php – To get the values inserted values by the front end page.

3. process.php - A PHP file that will process the request and will eventually link and insert the data.

Data gets inserted in a table which we will create in myphp admin named database. This table and database will get connected to the php page and the data will be transmitted.

phpMyadmin -

phpMyAdmin is a free and open source administration tool for MySQL.

It can run on local host as well on server host in the system. In this tutorial, we’re going to use this tool to insert data.

Steps to insert data –

  1. Firstly, we’ll create a database and table in phpMyadmin as per our need. The data we’ll insert will get stored in that database in tabular form.
  2. After creating a table, we’ll create a php file named ‘DBCON.php’ in which we’ll connect the front end php file with the database.

Code of ‘DBCON.php’ –

<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "name";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
?>
  1. In this code, we’ve defined server name, database name, it’s username and password along with variables.
  2. After saving this file, we will create a front end php file named ‘insert.php’.

in this file, we’re going to create a simple form with 3 fields and submit button to insert data and to connect it to the phpmyadmin tool in local server.

Code of ‘insert.php’ –

<!DOCTYPE html>
<html>
  <body>
 <form method="post" action="process.php">
  Name:<br>
  <input type="text" name="name">
  <br>
  City:<br>
  <input type="text" name="city">
  <br>
  Age:<br>
  <input type="text" name="age">
  <br><br>
  <input type="submit" name="save" value="submit">
 </form>
  </body>
</html>
  1. In this form, we’ve created 3 fields to be filled and in the form action, we’ve selected ‘insert’ page in order to link and redirect our form to the process page.
  2. After saving this file, here comes the final step where we will specify the data insertion and store it in variables.
  3. Now, we’ll create the file named ‘process' in which we’ll store the data in variables and it’ll insert the data into database.

Process.php Code –

<?php
include_once 'DBCON.php';
// database connectivity file
if(isset($_POST['save']))
{
$Name = $_POST['name'];
$City = $_POST['city'];
$Age = $_POST['age'];
$sql = "INSERT INTO data (name,city,age)
// data is the table name and then the fields in proper order in which we want to store data sequentially.
VALUES ('$name','$city','$age')";
// values stored in these variables
if (mysqli_query($conn, $sql))
// Connection query
{
echo "Data has been inserted";
// confirmation message
} else {
echo "Error Found, Try again" . $sql . "
" . mysqli_error($conn);
}
mysqli_close($conn);
// connection end
}
?>

In this code, we’re fetching data from the insert page and then inserting it into the database by using mySql commands.

Conclusion :-

In this tutorial, we’ve learned how to insert data in MySQL. Since MySQL is a sequence sensitive language, so data should be inserted in proper sequence.

All the three php coded files are important for data insertion. Each file has it’s specific code and connectivity. I hope this tutorial on how to insert data in MySQL using PHP helps you.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪