Insert data into the MySQL table using PHP.

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments
  • Post last modified:February 2, 2021
  • Reading time:5 mins read

Before we get started, How to insert data into the MySQL table using PHP? Now, we are using PHP for Back-end (i.e, Server-side) AND  Bootstrap 4 for Front end side (i.e, Client-side). Open the Xammp control panel to start the Apache server and MySQL. After that open any Text Editor (i.e, SublimeTextEditor, Notepad ++, Visual studio, Etc..).

Now open any Text Editor create a dot(.) PHP file(for example- form.php). write the Bootstrap 4 code that is given below and you can also visit W3schools and copy the MaxCDN & paste code after the title tag.

<html>
<head>
	<title>Data Insert Into Database Using PHP</title>
	<!--------------------------------MaxCDN----------------------------->	
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="card">
<div class="card-header">
<h4>DATA INSERT INTO DATABSE USING PHP</h4>
</div>
<div class="card-body">
<form action="<?php $_PHP_SELF?>" method="post">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>FULL NAME <span style="color: red;">*</span></label>
<input type="text" name="fullname" class="form-control" required="">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>EMAIL <span style="color: red;">*</span></label>
<input type="email" name="email" class="form-control" required="">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>PASSWORD<span style="color: red;">*</span></label>
<input type="password" name="password" class="form-control" required="">
</div>
</div>
<div class="col-md-12">
<div class="text-center">
				<input type="submit" name="submit" class="btn btn-primary">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
Insert data into the MySQL table

After that open browser and then open the PHPMyAdmin database (if local Server is used, then write http://localhost/phpmyadmin and press enter). and create a database (example- database name PHP and Database table name test).

Insert data into the MySQL table

After that next Question is how to connect with the database using PHP? Here is the simple code of database connection that is given below

$connection = mysqli_connect("hostname","Username","database password","database name");

when the database base connection is established then all the elements can be inserted into the database using the isset() function with help of the POST Method. Here is the PHP code –

<?php
$connection=mysqli_connect("localhost","root","","php");
if ($connection) {
if (isset($_POST['submit'])) {
$fullname=$_POST['fullname'];
$email=$_POST['email'];
$password=$_POST['password'];
////SET CURRENT TIME and DATE///////
date_default_timezone_set("Asia/Calcutta");
$date= date("Y/m/d H:i:s");

$sql="INSERT INTO test (fullname,email,password,entry_date) VALUES('$fullname','$email','$password','$date')";
				
$done=$connection->query($sql);
if ($done) {
echo "DATA INSERT SUCCESSFULL";
}else{
echo "SOMETHING WENT WRONG";
}
}
}else{
echo "Connection Faild";
}
?>
Here Is the complete code insert data into the MySQL table using PHP.
<?php
$connection=mysqli_connect("localhost","root","","php");
if ($connection) {
if (isset($_POST['submit'])) {
$fullname=$_POST['fullname'];
$email=$_POST['email'];
$password=$_POST['password'];
////SET CURRENT TIME and DATE///////
date_default_timezone_set("Asia/Calcutta");
$date= date("Y/m/d H:i:s");


$sql="INSERT INTO test (fullname,email,password,entry_date) VALUES('$fullname','$email','$password','$date')";
				
$done=$connection->query($sql);
if ($done) {
echo "DATA INSERT SUCCESSFULL";
}else{
echo "SOMETHING WENT WRONG";
}
}
}else{
echo "Connection Faild";
}
?>

<!DOCTYPE html>
<html>
<head>
	<title>Data Insert Into Database Using PHP</title>
	<!---------------------------------MaxCDN----------------------------------------------->
	<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="card">
<div class="card-header">
<h4>DATA INSERT INTO DATABSE USING PHP</h4>
</div>
<div class="card-body">
<form action="<?php $_PHP_SELF?>" method="post">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>FULL NAME <span style="color: red;">*</span></label>
<input type="text" name="fullname" class="form-control" required="">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>EMAIL <span style="color: red;">*</span></label>
<input type="email" name="email" class="form-control" required="">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>PASSWORD<span style="color: red;">*</span></label>
<input type="password" name="password" class="form-control" required="">
</div>
</div>
<div class="col-md-12">
<div class="text-center">
<input type="submit" name="submit" class="btn btn-primary">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
Insert data into the MySQL table

Leave a Reply