Server Side Form Validation in PHP and MySQL

Server-side form validation is the process of validating user input on the server side before it is sent to the database. This is done to ensure that the data is valid and meets the requirements of the application.

The most common way to do server-side form validation is to use PHP and MySQL. PHP is a scripting language that can be used to validate user input on the server side. MySQL is a database system that can store the data that is submitted by the user.

To validate user input using PHP and MySQL, the following steps should be taken:

1. Create a form with the necessary fields.

2. Create a PHP script to validate the user input. This script should check for required fields, valid data types, and any other criteria that must be met.

3. Connect to the MySQL database and store the user input in the appropriate tables.

4. Create a PHP script to retrieve the data from the database and display it on the page.

5. Create a PHP script to update the database with any changes made by the user.

By using PHP and MySQL, server-side form validation can be done quickly and easily. This ensures that the data is valid and meets the requirements of the application.
[ad_1]

Server side validation on forms in PHP and MySQL. In this tutorial; you will learn how to add or use server-side validation rules on HTML forms in PHP with MySQL database.

This tutorial will create a simple user registration form and add server-side validation to it using PHP and MySQL. And As well as validate and store form data in the MySQL database in PHP. And you can download the free source code of server side form validation php source php code.

How to Validate Form Data on Server Side in PHP MySQL

Let’s follow the following steps to validate form data on the server side in PHP MySQL:

  • Step 1 – Create a PHP Project
  • Step 2 – Setup Database with PHP Project
  • Step 3 – Create an HTML form
  • Step 4 – Add Server Side Validation with Form

Step 2 – Create a PHP Project

Visit your web server directory and create PHP project.

Step 2 – Setup Database with PHP Project

Create a file name db.php and update the below code into your file.

The below code is used to create a MySQL database connection in PHP. When we insert form data into MySQL database, there we will include this file:

<?php
	$servername='localhost';
	$username='root';
	$password='';
	$dbname = "my_db";
	$conn=mysqli_connect($servername,$username,$password,"$dbname");
	  if(!$conn){
		  die('Could not Connect MySql Server:' .mysql_error());
		}
?>

Step 3 – Create an HTML form

Create html form; so create index.php file and add following code into it:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Server side validation in PHP | Tutsmake.com</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-8 col-offset-2">
<div class="page-header">
<h2>Registration Form in PHP with Validation</h2>
</div>
<p>Please fill all fields in the form</p>
<form action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]); ?>" method="post">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" value="" maxlength="50" required=""> <span class="text-danger"><?php if (isset($name_error)) echo $name_error; ?></span>
</div>
<div class="form-group ">
<label>Email</label>
<input type="email" name="email" class="form-control" value="" maxlength="30" required=""> <span class="text-danger"><?php if (isset($email_error)) echo $email_error; ?></span>
</div>
<div class="form-group">
<label>Mobile</label>
<input type="text" name="mobile" class="form-control" value="" maxlength="12" required=""> <span class="text-danger"><?php if (isset($mobile_error)) echo $mobile_error; ?></span>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" value="" maxlength="8" required=""> <span class="text-danger"><?php if (isset($password_error)) echo $password_error; ?></span>
</div>
<div class="form-group">
<label>Confirm Password</label>
<input type="password" name="cpassword" class="form-control" value="" maxlength="8" required=""> <span class="text-danger"><?php if (isset($cpassword_error)) echo $cpassword_error; ?></span>
</div>
<input type="submit" class="btn btn-primary" name="signup" value="submit">Already have a account?<a href="login.php" class="btn btn-default">Login</a>
</form>
</div>
</div>
</div>
</body>
</html>

Step 4 – Add Server Side Validation with Form

Then open again your html file and add the following validation rules into it:

<?php
require_once "db.php";
if (isset($_POST['signup'])) {
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$cpassword = mysqli_real_escape_string($conn, $_POST['cpassword']); 
if (!preg_match("/^[a-zA-Z ]+$/",$name)) {
$name_error = "Name must contain only alphabets and space";
}
if(!filter_var($email,FILTER_VALIDATE_EMAIL)) {
$email_error = "Please Enter Valid Email ID";
}
if(strlen($password) < 6) {
$password_error = "Password must be minimum of 6 characters";
}       
if(strlen($mobile) < 10) {
$mobile_error = "Mobile number must be minimum of 10 characters";
}
if($password != $cpassword) {
$cpassword_error = "Password and Confirm Password doesn't match";
}
if (!$error) {
if(mysqli_query($conn, "INSERT INTO users(name, email, mobile ,password) VALUES('" . $name . "', '" . $email . "', '" . $mobile . "', '" . md5($password) . "')")) {
header("location: index.php");
exit();
} else {
echo "Error: " . $sql . "" . mysqli_error($conn);
}
}
mysqli_close($conn);
}
?>

Conclusion

Server side validation in PHP; In this tutorial, you have learned how to create a simple form and add server side validation in php.

Recommended PHP Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00