Insert Data in Database Using PHP MySQLi

Insert Data in Database Using PHP MySQLi

In this tutorial you will learn how to insert data in database using PHP MySQLi.

In MySQLi database you have to created a database named school. Then you have to create a table named students in school database which consists of six fields namely id, name, email, class, mobile and address.

SQL Code:

Here is source code of SQL you can create database and table from this code.

CREATE TABLE students(
	name varchar(255) NOT NULL,
	email varchar(255) NOT NULL,
	class varchar(50) NOT NULL,
	mobile varchar(20) NOT NULL,
	address varchar(255) NOT NULL

HTML Code:

Here is source code of HTML we have use bootstrap for design the form.

<!doctype html>
<html lang="en">
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="">

    <title>Registration Form</title>
	<div class="container">
		<div class="row justify-content-md-center">
			<div class="col-sm-6">
				<div class="card border-primary mt-5 mx-auto">
				  <div class="card-header text-white bg-primary">
					<b>Registration Form</b>
				  <div class="card-body">
					<form autocomplete="off" method="post" action="insert.php">
							<div class="form-group">
								<input type="text" class="form-control" name="name" placeholder="Enter Name">
							<div class="form-group">
								<input type="text" class="form-control" name="class" placeholder="Enter Class">
							<div class="form-group">
								<input type="email" class="form-control" name="email" placeholder="Enter Email">
							<div class="form-group">
								<input type="text" class="form-control" name="mobile" placeholder="Enter Mobile Number">
							<div class="form-group">
								<textarea class="form-control" name="address" placeholder="Enter Address"></textarea>
							<button type="submit" name="save" class="btn btn-primary btn-block">Save</button>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>

Form Design:

This form designed with HTML and Bootstrap. Source code of this form have provide above.

Registration Form Design

PHP Code:

Here is source code of PHP. With the help of this code you can insert data in MySQLi  database

	$con = mysqli_connect("localhost", "root", "", "school");
		$name = $_POST['name'];
		$class = $_POST['class'];
		$email = $_POST['email'];
		$mobile = $_POST['mobile'];
		$address = $_POST['address'];
		$sql = "insert into students(name, class ,email, mobile, address) values ('$name', '$class', '$email', '$mobile', '$address')";
		$run = mysqli_query($con, $sql);
		if ($run) {
			echo "Registration successfully";

Leave a Reply

Close Menu