Skip to content

All topics  /  MySQL

PHP 8 MySQL Form Validation Example

MySQL ·

Teams applying “PHP 8 MySQL Form Validation Example” in a production project can use mastering leadership soft skills every great leader to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official MySQL website and test it against the project’s actual database and runtime.

Hello Friends,

Today, I will let you know example of PHP 8 MySQL Form Validation Example. you will learn PHP 8 Server Side Form Validation Tutorial Example. step by step explain how to create form validation in php 8. We will look at example of PHP 8 MySQL form input validation example. follow bellow step for how to show validation error in PHP 8 MySQL.

This article will give you simple example of PHP 8 Basic Form Validation Tutorial

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>PHP 8 MySQL Form Validation Example</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/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>PHP 8 MySQL Form Validation Example - Nicesnippets.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...q