Here we are going to check how to create a php form and insert its values to database table.

In this example we will be submitting the php form to same page.

Form HTML :

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <div class="form-group">
        <label>Full Name:</label>
        <input class="form-control" type="text" name="fullname" required placeholder="Enter Your Full Name"/>
        <span class="Error"></span>
     </div>
     <div class="form-group">
         <label>Email:</label>
         <input class="form-control" type="email" name="email" required placeholder="Enter Your Email"/>
         <span class="Error"></span>
     </div>
     <div class="form-group">
         <label>Password:</label>
         <input class="form-control" type="password" name="password" required placeholder="Enter Password"/>
         <span class="Error"></span>
      </div>
      <div class="form-group">
          <label>Gender:</label><br/>
          <label><input type="radio" name="gender" required value="Male" checked /> Male</label>
          <label><input type="radio" name="gender" required value="Female" /> Female</label>
          <label><input type="radio" name="gender" required value="Other" /> Other</label>
          <span class="Error"></span>
       </div>
       <div class="form-group">
           <input class="btn btn-primary btn-block" name="submit" type="submit" value="Submit"/>
       </div>
  </form>
  • Here action is set to same page using <?php echo $_SERVER[‘PHP_SELF’]; ?>. Click here to know more abount PHP_SELF.
  • Method is set to POST, Click on PHP Form Methods to know in detail.
  • Required form elements like text for name, email for email, password for password and radio button for gender is given.
  • Input type submit is used for submitting the form.

After filling and submitting the form, all details are posted to the same file because we are using action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>”. If you want to post the details to a different file mention the filename in action that is action=”submitForm.php”.

Database :

We need to connect to database to store these form data in a table. For this open your phpmyadmin, create a database with appropriate name.

After creating database, create a table in the same database to store this form data. Check below create query.

Create Table :

CREATE TABLE `users` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) NOT NULL,
 `email` varchar(100) NOT NULL,
 `password` varchar(100) NOT NULL,
 `gender` varchar(100) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
  • users is the table name
  • id is the primary key of users table, it is set to not null and auto increment. int(11) is the datatype and 11 is the length
  • name, email, password and gender are the other fields
  • varchar is the datatype of other fields and its maximum length is 255

Database Connection :

After creating database make the database connection using the following syntax.

$con = mysqli_connect('localhost', 'root', '', 'form_database');
  • mysqli_connect is used to connect with database. Click here for more info.
  • localhost is the hostname
  • root is the username
  • password is null in case of localhost
  • form_database is the name of database

PHP Code to insert :

if(isset($_POST['submit']))
{
     $name = $_POST['fullname'];
     $email = $_POST['email'];
     $password = $_POST['password'];
     $gender = $_POST['gender'];

     $sql = "INSERT into users(name, email, password, gender) VALUES ('".$name."', '".$email."', '".$password."', '".$gender."');";
     $execute = mysqli_query($con, $sql);
     $success = 'User added successfully';
     $_POST = array();
}
  • As we are posting to the same file, if(isset($_POST[‘submit’])) {} is mandatory because the insert process should happen only if form submitted thus this if condition will check whether the form is submitted or not. ‘submit’ is the name of submit element.
  • All the values are stored in a variable, $name = $_POST[‘fullname’]; means $name is the variable, fullname is the elemnt name specified in form and $_POST[‘fullname’] retrieves the value which we posted.
  • In $sql the insert query is written
  • $execute = mysqli_query($con, $sql); this line will execute the insert query where $con is the connection variable and $sql is the query variable
  • In $success a success message is written and it is displayed like below
<?php if($success!=''){ ?>
     <div class="alert alert-success alert-dismissible">
         <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
         <strong>Success!</strong> <?php echo $success; ?>
     </div>
<?php } ?>
  • This is a simple bootstrap alert message design
  • $_POST = array(); will clear the post array otherwise when we refresh all data will get insert again and again creating duplicate data

Full Code :

Here is the full code for php form program in php

<?php
$con = mysqli_connect('localhost', 'root', '', 'joby_practice');
$success = '';
if(isset($_POST['submit']))
{
	$name = $_POST['fullname'];
	$email = $_POST['email'];
	$password = $_POST['password'];
	$gender = $_POST['gender'];

	$sql = "INSERT into users(name, email, password, gender) VALUES ('".$name."', '".$email."', '".$password."', '".$gender."');";
	$execute = mysqli_query($con, $sql);
	$success = 'User added successfully';
		
}
?>
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
</head>
<body>
	<div class="container col-6 m-5">
       <div class="Back">
            <i class="fa fa-arrow-left" onclick="Back()"></i>
        </div>

        <?php if($success!=''){ ?>
        <div class="alert alert-success alert-dismissible">
        	<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
		  	<strong>Success!</strong> <?php echo $success; ?>
		</div>
		<?php } ?>
        <p class="h2 text-center">Form</p>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            
            <div class="form-group">
                <label>Full Name:</label>
                <input class="form-control" type="text" name="fullname" required placeholder="Enter Your Full Name"/>
                <span class="Error"></span>
            </div>
            <div class="form-group">
                <label>Email:</label>
                <input class="form-control" type="email" name="email" required placeholder="Enter Your Email"/>
                <span class="Error"></span>
            </div>
            <div class="form-group">
                <label>Password:</label>
                <input class="form-control" type="password" name="password" required placeholder="Enter Password"/>
                <span class="Error"></span>
            </div>
            <div class="form-group">
                <label>Gender:</label><br/>
                <label><input type="radio" name="gender" required value="Male" checked /> Male</label>
                <label><input type="radio" name="gender" required value="Female" /> Female</label>
                <label><input type="radio" name="gender" required value="Other" /> Other</label>
                <span class="Error"></span>
            </div>
            <div class="form-group">
                <input class="btn btn-primary btn-block" name="submit" type="submit" value="Submit"/>
            </div>
        </form>
    </div>
</body>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
</html>

Included Files :

Here I have included 3 files, bootstrap css & js and jquery

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>

Hit me to get more php solutions.

Hope this php form in php tutorial helps you.

Leave A Comment