Form Submit in PHP

Form submit in php means you are going to get input from user and saving data in database table so there will be three points to understand making html form, creating MySQL database table and finally receiving data from form and insert in database table.

In first step let's create a simple html form you may get a beautiful form design from internet but now my target to explaining basic form concept so making less design form let's create a user registration form with five input fields username, password, email, phone and address.

Let's create a file form.html (you may give it name form.php) and save in your root or any folder where you want to put this example.

<html>
<head>
</head>
<body>
<form action="receive.php" method="POST" >
<input name="user_name" value="" type="text" required /> <br />
<input name="password" value="" type="text" required /> <br />
<input name="email" value="" type="text" required /> <br />
<input name="phone" value="" type="text" /> <br />
<input name="address" value="" type="text" /> <br />
<input type="submit" name="Submit" Value="Submit" />
</form>
</body>
</html>

A simple html form so you understand well as you see i make first three field required so these field will not submit empty but user can skip filling last two fields phone and address. In the form action i give a file name receive.php this file will receive data when click on submit button. Actually, when the submit button clicks after checking required fields all the five data fields will be through on receive.php and in this file will receive these fields.

Before moving let's create a table named "users" in the database so that we can save this data after receiving it.

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `user_name` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

Just run above quarries in phpMyAdmin SQL block and create the "users" table having 6 fields as id is primary key i have created, we need one primary key to unique access to all records.

Now let's create receive.php as we know we have a plan to save data in MySQL that's mean we should have connection with MySQL database so at the top of receive.php we will make a connection for insert record.

<?php
$server_name = "localhost";
$db_user_name = "username";
$db_password = "password";
$db_name = "test";

// Create connection
$con = mysqli_connect($server_name , $db_user_name , $db_password, $db_name );

// Check connection
if (!$con) {
  die("Connection Error: " . mysqli_connect_error());
}

?>

The above code will be on top of receive.php and if connection failed then due to use of die command script will stop working next and error will show here so we will check the error and fix it.

After click of submit button in form receive.php will be called and at the top connection with database check if connection is ok then script running will continue and we will form fields.

<?php
$user_name= $_POST['user_name'];
$password= $_POST['password'];
$email= $_POST['email'];
$phone= $_POST['phone'];
$address= $_POST['address'];
?>

After checking database connection, we received the five fields and saved in variable for use here you may print the field for testing purpose so you are clear that fields received properly just use simple echo field to print these fields. It is only for tests.

echo("User Name: ". $user_name. "<br />");
echo("Password: ". $password. "<br />");
echo("Email: ". $email. "<br />");
echo("Phone: ". $phone. "<br />");
echo("Address: ". $address. "<br />");

If you are getting output what you fill in form then it's time to move next and insert this record in database table users. First, we need to create SQL query which contains all these field variables. Now you are going to insert record and that is actually form submit in php.

$sql = "INSERT INTO users (`user_name`, `password`, `email`, `phone`, `address`) VALUES ('".$user_name."', '".$password."', '".$email."', '".$phone."', '".$address."')";
if(mysqli_query($con, $sql)){
    echo "User created successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
}

First question in your mind we have 6 fields in table but i am inserting 5 fields and id is missing why? "id" we have created an auto increments field, if we don't give any value then it automatically gets an updated value by inserting one in the previous record if it is first record then it will be one value as it is int field. When running a query there is $con which we have created at the top of this file so if we see all code in box then file receive.pp will be.

<?php
$server_name = "localhost";
$db_user_name = "username";
$db_password = "password";

// Create connection
$con = mysqli_connect($server_name , $db_user_name , $db_password );

// Check connection
if (!$con) {
  die("Connection Error: " . mysqli_connect_error());
}

// receiving form fields
$user_name= $_POST['user_name'];
$password= $_POST['password'];
$email= $_POST['email'];
$phone= $_POST['phone'];
$address= $_POST['address'];

// printing all fields only for testing.
echo("User Name: ". $user_name. "<br />");
echo("Password: ". $password. "<br />");
echo("Email: ". $email. "<br />");
echo("Phone: ". $phone. "<br />");
echo("Address: ". $address. "<br />");

// running insert query
$sql = "INSERT INTO users (`user_name`, `password`, `email`, `phone`, `address`) VALUES ('".$user_name."', '".$password."', '".$email."', '".$phone."', '".$address."')";
if(mysqli_query($con, $sql)){
    echo "User created successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
}