How to Add Server Side Validation Rules In PHP MySQL?

03-Apr-2023

.

Admin

How to Add Server Side Validation Rules In PHP MySQL?

Hi Dev,

This tutorial is focused on how to add or use server-side validation rules on HTML forms in PHP MySQL. you'll learn how to add or use server-side validation rules on HTML forms in PHP with MySQL database. We will use how to add server-side validation rules on HTML forms in PHP MySQL. if you have a question about how to add server-side validation rules on HTML forms in PHP with MySQL then I will give a simple example with a solution.

Now, let's see the article on how to add server-side validation rules on HTML forms in PHP with MySQL. it's a simple example of how to add server-side validation rules on HTML forms in PHP MySQL. you can understand the concept of how to add or use server-side validation rules on HTML forms in PHP with MySQL database. if you have a question about how to add or use server-side validation rules on HTML forms in PHP MySQL then I will give a simple example with a solution.

Step 1: Create PHP Project


Visit your web server directory and create a PHP project.

Step 2: Create a Database Connection 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 a HTML form

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Server side validation in PHP</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

<?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);

}

?>

I hope it could help you...

#PHP