Insert Data into MySQL Database using PHP

Welcome to the third part of the tutorial. You will learn how to use PHP to INSERT data into MySQL database through an HTML Form. First of all, you should check out our other tutorial which covers the most important steps of

1. How to Set Up Local Server / Localhost Using XAMPP (first tutorial)

2. How to Create MySQL Database using phpMyAdmin (second tutorial)

In this tutorial we are using the same database (Afterlecture) that we made in our second tutorial which contain fields like Id, Name and Email. 

table in sql

Step by Step Procedure to Insert Data into MySQL Database using PHP

Step 1-> Establish Connection with MySQL Database

Code

<?php
$host = ‘localhost’;
$username = ‘root’;
$password = ”;
$dbname = ‘AfterLecture’;
@mysql_connect($host,$username,$password) or die(“Please check username or password”);
@mysql_select_db($dbname) or die(“Not connected to database”);
?>

Explanation of the above code

The main method used in this script is mysql_connect(). This is an internal PHP function, you can read more about it.

At the very beginning of our code, we see few variable declarations and values assigned to those variables. Usually, we need four variables to establish a proper connection: $host, $username, $password and  $dbname(). In the code, we have set our exact database details as values to those variables and later, in the function mysql_connect() we use those variables to pass onto the function.

A function die() is executed here, which basically kills our script and gives us a message that we have set. So this will by default say Connection failed: and an exact error message will follow to help us determine the issue, like in this code the message that user will get if the connection is not establish with the database is, Not connected to database.

Note: Save this file with any name but here we are saving it with name connection.php. Paste it inside c:/xampp/htdocs/afterlecture 

*Here we have  made a folder with a name afterlecture, you can paste the file connection.php  directly in htdocs also. If you are pasting it in htdcocs folder directly than you can simply run the program by writing localhost/connection.php  in the URL.

Step 2 ->Make an HTML form which contains as many fields as you required, but here we have made two fields and a submit button like below :

indexPhp

Save the file with the name index.php and paste it inside the same folder c:/htdocs/afterlecture If you don’t want to make a folder inside htdocs than you can simple paste your files directly to htdocs folder. In that case you have to paste it inside c:/htdocs/

Tip: For those who are using WAMPP, you will find a folder name www inside c:/wampp/, and the same procedure can be follow with www folder.

Now you have two files inside the folder Afterlecture like below :

afterlecture folder inside htdocs

index.php code

<?php
include_once(‘connection.php’);
if(isset($_POST[‘submit’]))
{
// Get values from form
$name = $_POST[‘name’];
$email = $_POST[’email’];
// Insert data into mysql
if(mysql_query(“insert into Student(name,email)VALUES(‘$name’,’$email’)”))
{
// if successfully insert data into database, displays message “Successful”.
echo “Data Submitted Succefully”;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset=”UTF-8″>
<title>AfterLecture.com | Ask Your Questions Here</title>
</head>
<body>
<form action=”” method=”post”>
<p>Name: <input type=”text” name=”name” id=””></p>
<p>Email: <input type=”email” name=”email” id=””></p>
<p><input type=”submit” name=”submit”></p>
</form>
</body>
</html>

insert data into mysql using php

As you can see the new data has been inserted into the database and you can also see this data on the same form page.

Note: The above code is for inserting the data to the database . For retrieving the data from the data just stay tuned we will make another post for it. If you face any difficulties to insert data into MySQL than feel free to ask your doubts in the comment section.

3 thoughts on “Insert Data into MySQL Database using PHP

Leave a Reply

Your email address will not be published. Required fields are marked *