How to Create Form Validation in PHP 8 MySQL?
May 27, 2022 . Admin
Hello Friends,
In this tutorial, you will learn How to Create Form Validation in PHP 8 MySQL. I’m going to show you about PHP 8 MySQL Form Validation Example. This post will give you simple example of how to create form validation in php 8. I explained simply step by step PHP 8 MySQL form input validation example.
I will give you simple example of PHP 8 Server Side Form Validation Tutorial Example.
So, let's see bellow solution:
index.php<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>How to Create Form Validation in PHP 8 MySQL? - Mywebtuts.com</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous"> </head> <body> <?php $con = mysqli_connect("localhost","root","","form_validations"); $errors = array(); if($_SERVER['REQUEST_METHOD'] == 'POST'){ if(preg_match("/\S+/", $_POST['fname']) === 0){ $errors['fname'] = "* First Name is required."; } if(preg_match("/\S+/", $_POST['description']) === 0){ $errors['description'] = "* Message filed is required."; } if(preg_match("/.+@.+\..+/", $_POST['email']) === 0){ $errors['email'] = "* Not a valid e-mail address."; } if(preg_match("/.{8,}/", $_POST['password']) === 0){ $errors['password'] = "* Password Must Contain at least 8 Chanacters."; } if(strcmp($_POST['password'], $_POST['confirm_password'])){ $errors['confirm_password'] = "* Password do not much."; } if(count($errors) === 0){ $fname = mysqli_real_escape_string($con, $_POST['fname']); $description = mysqli_real_escape_string($con, $_POST['description']); $email = mysqli_real_escape_string($con, $_POST['email']); $password = hash('sha256', $_POST['password']); function createSalt(){ $string = md5(uniqid(rand(), true)); return substr($string, 0, 3); } $salt = createSalt(); $password = hash('sha256', $salt . $password); $search_query = mysqli_query($con, "SELECT * FROM members WHERE email = '$email'"); $num_row = mysqli_num_rows($search_query); if($num_row >= 1){ $errors['email'] = "Email address is unavailable."; }else{ $sql = "INSERT INTO members(`fname`, `description`, `email`, `salt`, `password`) VALUES ('$fname', '$description', '$email', '$salt', '$password')"; $query = mysqli_query($con, $sql); $_POST['fname'] = ''; $_POST['description'] = ''; $_POST['email'] = ''; $successful = "<h4 class='alert alert-success'> You are successfully registered.</h4>"; } } } ?> <div class="container mt-5"> <div class="row"> <div class="col-md-12 "> <div class="card w-75 m-auto"> <div class="card-header text-center bg-dark text-white"> <h3>How to Create Form Validation in PHP 8 MySQL? - Mywebtuts.com</h3> </div> <div class="card-body"> <form method="post" action=""> <?php if(isset($successful)){ echo $successful; } ?> <div class="row"> <div class="col-md-6"> <div class="form-group"> <label for="fname"><strong>First Name :<span class="text-danger">*</span></strong></label> <input type="text" class="form-control" name="fname" id="fname" placeholder="First Name" value="<?php if(isset($_POST['fname'])){echo $_POST['fname'];} ?>"> <?php if(isset($errors['fname'])){echo "<span class='text-danger'>" .$errors['fname']. "</span>"; } ?> </div> </div> <div class="col-md-6"> <div class="form-group"> <label for="email"><strong>Email :<span class="text-danger">*</span></strong></label> <input type="text" class="form-control" name="email" id="email" placeholder="E-mail Address" value="<?php if(isset($_POST['email'])){echo $_POST['email'];} ?>"> <?php if(isset($errors['email'])){echo "<span class='text-danger'>" .$errors['email']. "</span>"; } ?> </div> </div> <div class="col-md-6"> <div class="form-group"> <label for="pw"><strong>Password :<span class="text-danger">*</span></strong></label> <input type="password" class="form-control" name="password" id="pw" placeholder="Password"> <?php if(isset($errors['password'])){echo "<span class='text-danger'>" .$errors['password']. "</span>"; } ?> </div> </div> <div class="col-md-6"> <div class="form-group"> <label for="cpw"><strong>Confirm Password :<span class="text-danger">*</span></strong></label> <input type="password" class="form-control" name="confirm_password" id="cpw" placeholder="Confirm Password"> <?php if(isset($errors['confirm_password'])){echo "<span class='text-danger'>" .$errors['confirm_password']. "</span>"; } ?> </div> </div> <div class="col-md-12 "> <div class="form-group"> <label for="description"><strong>Message :<span class="text-danger">*</span></strong></label> <textarea type="text" rows="5" class="form-control" name="description" id="description" placeholder="Message"></textarea> <?php if(isset($errors['description'])){echo "<span class='text-danger'>" .$errors['description']. "</span>"; } ?> </div> </div> </div> <div class="form-group d-flex justify-content-center"> <input type="submit" name="submit" class="btn btn-success" value="Submit"> </div> </form> </div> </div> </div> </div> </div> </body> </html>Output:
It will help you...