All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Form Validation And Submit To Database

Last Updated : Mar 11, 2024

PHP Form Validation And Submit To Database

In this tutorial we will show you the solution of PHP form validation and submit to database, here first we need to create form with some input fields with submit button user submits form ‘action’ attribute loads external script file.

There we validating each fields input if all input had correct inputs then we inserting all form’s data to table ‘loginrec’ in database ‘dbase’ if database connection exist it successfully inserted to database table otherwise it throws error.

Step By Step Guide On PHP Form Validation And Submit To Database :-

Here we created form with action, method attributes and in action we inserted signup script file for take next action when user submits form, method with post value give permission to access user entered values on external file.

In form we collecting details of ‘username,email,password’ from users then when user submit their form action will loads signup.php file.

In signup file we collected each input field values by ‘post’ then we checks if all input fields whether filled or not by user if they not filled any one field then it will throws message ‘Please fill all required fields!’ otherwise it checks password.

If password also matched then we executes database connection and user inputs are inserted to database successfully.

<html>
<body>
<form action="signup.php" method="POST">
  Username: <input type="text" name="username" /><br />
  Email: <input type="text" name="email" /><br />
  Password: <input type="text" name="password" /><br />
  Confirm password: <input type="text" name="password_confirm" /><br />
<input type="submit" value="Register" />
</form>
</body>
</html>

Dbcon.php

<?php
    $hName='localhost';
    $uName='root';
    $password='';
    $dbName = "dbase";
    $dbCon = mysqli_connect($hName,$uName,$password,"$dbName");
     if(!$dbCon){
          die('Could not Connect MySql Server:' .mysql_error());
      }
?>

Signup.php

<?php
$un=$_POST['username'];
$psw=$_POST['password'];
$em=$_POST['email'];
$pc=$_POST['password_confirm'];
if(empty($un) ||
    empty($psw) ||
    empty($em) ||
    empty($pc)) {
    die('Please fill all required fields!');
}
else{
if($_POST['password'] !== $_POST['password_confirm']) {
   die('Password and Confirm password should match!');
}
else{
    include_once 'dbcon.php';
    $sql="INSERT INTO loginrec VALUES ('$un','$psw','$em')";
    if($dbCon->query($sql) === TRUE){
    echo "Form Validated and Data Submitted to Database Successfully";
    }
    else{
        echo $dbCon->error;
    }
}
}
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we defined form with two attributes ‘action,method’. Action used for load external signup.php file then within that we defined four input fields for collects username, email, password, confirm password and register button.
  4. When user entering all details then clicks ‘Register’ button next action will loads external signup.php file.
  5. In php file we collecting ‘username,password, email, password_confirm’ details by $_POST[] then we stored respective variables ‘$un,$psw,$em,$pc’.
  6. Using if() condition we checks whether all input fields filled by user or not by empty() method. It will returns Boolean value if it returns true then it throws message ‘Please fill all required fields!’.
  7. If empty() method returns false then we need to check both passwords if both are matched we using dbcon.php file for executes database ‘dbase’ connection.
  8. We defined insert query for insert into table ‘loginrec’ and stored to variable ‘$sql’. Then using if() condition we checking whether database had ‘loginrec’ table connection if it is available then throws message ‘Form Validated and Data Submitted to Database Successfully’ and successfully inserted to table.
  9. Otherwise it throws database connection error on webpage.

Conclusion :-

In conclusion we are able to know how validate forms and submit to database using php.

First we need to start our xampp server then we load this program on browser we can see result of Register form with input fields.

When user entered all details then clicks register button it will successfully inserted on database otherwise it had some error when user filling the form so it throws respective error message on webpage for instruct user to modify them.

I hope this tutorial on PHP form validation and submit to database helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪