How to Insert Data into Database
Project : In this project we will learn how to insert data using form in
database.
myproject/index.php
index.php
<?php include("insert.php"); ?>
<?php include("common/header.php"); ?>
<body>
<div class="container-fluid" style="margin-top: 50px;">
<?php include("common/navbar.php"); ?>
<div class="text-center"> <?php echo $success; ?> <?php echo $error; ?> </div>
<div class="container w-50">
<form method="POST" action="form.php" class="border p-4">
<h3 class="rounded bg-primary w-100 p-2 text-white text-center">Submit Data </h3>
<div class="form-group">
<label for="fname">First Name </label>
<input type="text" class="form-control" name="firstname" id="firstname">
</div>
<div class="form-group">
<label for="lname">Last Name </label>
<input type="text" class="form-control" name="lastname" id="lastname">
</div>
<div class="form-group">
<label for="username">Username </label>
<input type="text" class="form-control" name="username" id="username">
</div>
<div class="form-group">
<label for="email">Email </label>
<input type="text" class="form-control" name="email" id="email">
</div>
<div class="form-grou pb-2">
<input type="submit" name="submit" value="Submit" class="btn btn-success">
<a href="index.php" class="btn btn-danger float-right">Close </a>
</div>
</form>
</div>
</div>
<div class="copyright py-4 text-center text-white">
<div class="container "> <small>© Copyright 2020 by Saten Chauhan </small> </div>
</div>
</body>
<?php include("common/footer.php"); ?>
Check image below Insert Form
Create table in database
Table in database
CREATE TABLE `users` (
`id` int(11) NOT NULL PRIMARY KEY AUTO-INCREAMENT,
`fname` varchar(100) NOT NULL,
`lname` varchar(100) NOT NULL,
`username` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Create connection with database
myproject/dbcon.php
dbcon.php
$dbhost ="localhost";
$dbuser ="root";
$dbpswd ="";
$dbname ="dbdemo";
$dbcon= mysqli_connect($dbhost,$dbuser,$dbpswd,$dbname);
if($dbcon){
//echo "Connected............!!!";
return $dbcon;
exit();
}else{
die("Connection failed ? ? ?". mysqli_error($dbcon));
}
mysqli_close($dbcon);
Insert Data
Process for inserting data in database
myproject/insert.php
insert.php
<?php include("dbcon.php");
$error=''; $success='';
if(isset($_POST['submit'])){
$fname = mysqli_real_escape_string($dbcon,ucfirst($_POST['firstname']));
$lname = mysqli_real_escape_string($dbcon,ucfirst($_POST['lastname']));
$uname = mysqli_real_escape_string($dbcon,strtolower($_POST['username']));
$username = preg_replace('/\s+/','',$uname);
$email = mysqli_real_escape_string($dbcon,strtolower($_POST['email']));
// This is to check if user is already exists
$email_query = "SELECT email FROM users WHERE email= '$email'";
$check_email = mysqli_query($dbcon,$email_query);
if(empty($fname)){
$error="<div class='alert alert-danger' style='color:red;'>Please enter first name ! Required !!</div>";
}else if(empty($lname)){
$error="<div class='alert alert-danger' style='color:red;'>Please enter last name ! Required !!</div>";
}else if (!preg_match("/^[a-zA-Z]+$/", $fname)) {
$error="<div class='alert alert-danger' style='color:red;'>Numbers are not allowedd !!</div>";
}else if (!preg_match("/^[a-zA-Z]+$/", $lname)) {
$error="<div class='alert alert-danger' style='color:red;'>Numbers are not allowed !!</div>";
}else if(empty($uname)){
$error="<div class='alert alert-danger' style='color:red;'>Please enter username ! Required !!</div>";
}else if ($uname != $username) {
$error="<div class='alert alert-danger' style='color:red;'>Do not use space in username !!</div>";
}else if (strlen($uname) < 4 ) {
$error="<div class='alert alert-danger' style='color:red;'>The Username must be at least 4 characters !!</div>";
}else if (!preg_match("/^[a-zA-Z0-9.]+$/", $uname)) {
$error="<div class='alert alert-danger' style='color:red;'>The characters or symbols are not allowd !!</div>";
}else if(empty($email)){
$error="<div class='alert alert-danger' style='color:red;'>Please enter e-mail address ! Required !!</div>";
}else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error="<div class='alert alert-danger' style='color:red;'>The E-mail address is invalid !!</div>";
}else if(mysqli_num_rows($check_email) > 0 ){
$error="<div class='alert alert-danger' style='color:red;'>The E-mail address already registered !!</div>";
}else{
$sql="INSERT INTO users(fname,lname,username,email) VALUES('$fname','$lname','$uname','$email')";
$data = mysqli_query($dbcon,$sql);
if($data){
$success ="<div class='alert alert-success w-100' style='color:green;'>User has been submitted !</div>";
}else{
$error = "<div class='text-success alert alert-success'> OOPS! your registration failed </div>";
}
}
}
mysqli_close($dbcon);
//for message display you have to write div tag on the top of html form page
common
header.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,
shrink-to-fit=no">
<title>Codetechinfo</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
myproject/common/navbar.php
navbar.php
<nav class="navbar navbar-expand-lg navbar-light bg-primary text-white fixed-top">
<a class="navbar-brand text-white" href="./index.php">Learn PHP </a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link text-white" href="./index.php">Home </a>
</li>
</ul>
</div>
</nav>
<br><br>
footer.php
<div class="copyright py-4 text-center text-white">
<div class="container "><small>© Copyright 2025 by Saten Chauhan</small> </div>
</div>
</body>
</html>
css folder
You must download and include bootstrap css source files for good interface OR You can add cdn link from bootstrap website.
myproject/css
bootstrap.min.css