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 DATABASE IF NOT EXISTS school;
CREATE TABLE students(
	id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
	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">
  <head>
    <!-- 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="https://stackpath.bootstrapcdn.com/bootswatch/4.1.3/cosmo/bootstrap.min.css">

    <title>Registration Form</title>
  </head>
  <body>
	<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>
				  <div class="card-body">
					<form autocomplete="off" method="post" action="insert.php">
						<fieldset>
							<div class="form-group">
								<input type="text" class="form-control" name="name" placeholder="Enter Name">
							</div>
							
							<div class="form-group">
								<input type="text" class="form-control" name="class" placeholder="Enter Class">
							</div>
							
							<div class="form-group">
								<input type="email" class="form-control" name="email" placeholder="Enter Email">
							</div>
							
							<div class="form-group">
								<input type="text" class="form-control" name="mobile" placeholder="Enter Mobile Number">
							</div>
							
							<div class="form-group">
								<textarea class="form-control" name="address" placeholder="Enter Address"></textarea>
							</div>
							
							<button type="submit" name="save" class="btn btn-primary btn-block">Save</button>
						</fieldset>
					</form>
				  </div>
				</div>
			</div>
		</div>
	</div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
  </body>
</html>

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

<?php
	$con = mysqli_connect("localhost", "root", "", "school");
	
	if(isset($_POST['save']))
	{
		$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