How to insert data in database using PHP MySQL?

In this tutorial you will learn how to insert data in database using php MySQL? in easy way. Here I will explain each everything in step by step so that you can understand this tutorial easily.

Note: - If you found any mistakes or difficulty in this tutorial so please let me know. You can give view or comment in comment box or through feedback page.

What we are doing in this tutorial?

Step 1.

First we will create an insert design page in Php so that we can insert the data in database. Now create an insert data design page in Php like below design. My Php insert data design page name is insert-design.php.

PHP insert data in Database

Copy the below complete HTML code and paste it in insert-design.php page.

	
   <html>
   <head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <title>Insert Data - Campuslife</title>
   <style>
	body{background-color: #f5f5f5; margin: 0px; font-family: Arial;}
	.main-container{display: flex; justify-content: center; margin-top: 30px;}
	.content{background-color: #fff; width: 400px; border-radius: 3px; box-shadow: 0 .125rem .25rem rgba(0,0,0,.075)!important;}
	.header{background: url(https://kkarjun.com/Mix-Wallpaper/2.jpg) center; background-size: cover; border-top-left-radius: 3px; border-top-right-radius: 3px; color: #f7f7f7; height: 120px; display: flex; justify-content: flex-start; align-items: center; padding-left: 10px;}
	.header h2{margin: 0px; text-shadow: 0 3px 1px #000;}

	.control-box{padding: 30px 25px 30px 20px;}
	/*------------------- input filed section css ------------*/
	.cont-box{margin-bottom: 10px;}
	.cont-box .tbox{width: 100%; height: 40px; font-size: 0.9rem; border: 1px solid #dbdbdb; border-radius: 3px; padding-left: 5px; outline: none;}
	.cont-box .tbox:focus{border: 1px solid rgba(35,164,0,0.51);}
	.cont-box .tarea{width: 100%; height: 70px; resize: none; padding-left: 5px; padding-top: 5px; border: 1px solid #dbdbdb; font-size: 0.9rem; font-family: Arial; border-radius: 3px; outline: none;}
	.cont-box .tarea:focus{border: 1px solid rgba(35,164,0,0.51);}
	/*------------------ button section css -----------------*/
	.btn-box{margin-top: 10px; text-align: center;}
	.btn{height: 40px; border: none; cursor: pointer; border-radius: 3px; background-color: #23a465; 
	color: #f7f7f7; font-size: 1rem; font-weight: 600; width: 100%;}
	.error{color: #d14269; font-size: 0.9rem; padding-top: 5px; display: block;}
	.success{color: #23a465; font-size: 0.9rem; padding-top: 5px; display: block; text-align: center;}
   </style>
   </head>
 
   <body>
   <div class="main-container">
	<div class="content">
	    <div class="header">
		<h2>Insert Data in PHP using MySQL</h2>
	    </div>
	    <div class="control-box">
		<form method="post" id="myform" enctype="multipart/form-data">
		   <div class="cont-box">
			<input type="text" placeholder="Enter Full Name" name="fname" class="tbox" />
		   </div>
		   <div class="cont-box">
			<input type="text" placeholder="Enter Mobile No." name="mobile_no" class="tbox" />
		   </div>
		   <div class="cont-box">
			<textarea placeholder="Enter Any Message" name="msg" class="tarea"></textarea>
		   </div>
		   <div class="btn-box">
			<input type="submit" id="insert" value="Insert" name="btn_insert" class="btn" />
		   </div>
		   <div class="success">

		   </div>
		</form>
		</div>
	</div>
   </div>
   </body>
   </html>
	

Now our designing part has been completed and now we will follow the step 2.

Step 2.

After creating the design page now we will create database and table structure. Here I am creating database with name campuslife. Below is the command of creating the database.


	create database campuslife
	

Now we will use this database so that we can create table inside this campuslife database. Below is the command to use the campuslife database.


	use campuslife

Now we are inside the campuslife database and now we will create a table in this database with insert_tb name. Here I am creating a table with 4 field name, like id, name, mobile and msg.

The table id will generate automatically when we insert the data in table. In mysql we will set id (auto_increment), so that whenever we will insert data in table a unique id will generate automatic for every new record. For creating a table follow the below command.

	
	create table insert_tb
	(
		id int auto_increment primary key,
		name varchar(50),
		mobile varchar(50),
		msg varchar(100)
	)
	

id: column for generating the automatic unique id for every new record.

name: column for storing the name.

mobile: column for storing the mobile number.

msg: column for storing the message.

Why we are creating id?

Because we will use id for delete the record or data.

Step 3.

I am creating separate connection file so that I can access it anywhere in page. Now create a connection.php file. In this we will add connection information of MySQL so that we can create connection between PHP to MySQL for login.

Why separate connection file?

Because we can call that file in many pages. If we can’t create separate file for connection then we have to write same code in every pages manually. It is good for single or two pages, but not good for 10 or more pages.

Connection file contain below information like (host name, MySQL user name, MySQL password, database name). Change the connection information as your MySQL login information in connection.php file.

Copy the below code and paste it in connection.php page.


    <?php

	$host ="localhost";
	$uname = "root";
	$pwd = '123456';
	$db_name = "campuslife";

    ?>
	

Step 4.

Now create an insert data Php page to perform the insertion operation to insert the data in table. In Php we will use MySQL insert query to insert the data or records in database. My php page name is insert-code.php.

Copy the below complete Php code and paste it in insert-code.php page.


    <?php
	/*-- we included connection file--*/
	include("connection.php");
	$success = "";

	if(isset($_POST["btn_insert"]) == "Insert")
	{
	   /*---creating variables-----*/
	   $name = $_POST["fname"];
	   $mobile = $_POST["mobile_no"];
	   $msg = $_POST["msg"];

	   /* creating connection between php to mysql */
	   $result  = mysqli_connect($host, $uname, $pwd) or die("Could not connection to MySQL.". mysqli_error());

	   /* selecting database */
	   mysqli_select_db($result, $db_name) or die("Could not connect to Database.". mysqli_error());

	   /*inserting the data using mysql insert command using php mysql query function */
	   mysqli_query($result,"INSERT INTO insert_tb(name, mobile, msg) VALUES('$name', '$mobile', '$msg')") or die("could not insert the record.". mysqli_error($result));
	   $success = "Data Inserted Successfuly.";
	}
    ?>

Step 5.

Now add insert-code.php page in insert-design.php page at the top like below. Just copy the below code and paste it in your insert-design.php page at the top.

	
	<?php
	
		include('insert-code.php'); // Include insert code Script page.
		
	?>
	
PHP include function

Step 6.

Now we will show Php success message on design page. Whether data inserted or not in database. So copy the below complete code and paste it in insert-design.php page in between <div class="success"> ... </div> tag.

	
	<strong>
		<?php if(isset($success)){echo $success;}?>
	</strong>
	
	
	<div class="success">
		 Avobe complete code will come here...
	</div>
	

Step 7. Final Step

Now our task has been completed and now test your application on browser. Run the insert-design.php page and test your application.

I hope you liked this tutorial. If you found this tutorial is very easy to learn then please give one like and share this tutorial with your friends. Also give your view in comment box or give your feedback on feedback page.

Do you want Youtube channel of Campuslife

Please provide your suggestion on feedback page.

Subscribe Now

Post Your Comment