How to Create PHP 8 Contact Form With MySQL?

May 30, 2022 . Admin



Hello Friends,

Hello all! In this article, we will talk about How to Create PHP 8 Contact Form With MySQL?. this example will help you PHP Contact us Form with Database and Validation. This tutorial will give you simple example of PHP MySQL contact us form with validation using Bootstrap. it's simple example of Simple PHP Contact Form to Storing In MySQL Database. Follow bellow tutorial step of How to Create a Contact Page with PHP.

We will use get Simple Example of How to Creat a Simple Contact Form with PHP

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">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    
    <title>How to Create PHP 8 Contact Form With MySQL?</title>
    
    <style type="text/css">
        .error
        {
            color: red;
        }
    </style>
    
</head>
<body>

<?php 
$nameErr = $emailErr = $passwordErr = $confirm_passwordErr = $messageErr = "";
$name = $email = $password = $confirm_password = $message = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST['name'])) {
        $nameErr = "Name is required!";
    }
    
    if (empty($_POST['email'])) {
        $emailErr = "Email is required!";
    }

    if (empty($_POST['password'])) {
        $passwordErr = "Password is required!";
    }

    if (empty($_POST['confirm_password'])) {
        $confirm_passwordErr = "Confirm_password is required!";
    }

    if (empty($_POST['message'])) {
        $messageErr = "Message is required!";
    }

    if($_POST['password']!=$_POST['confirm_password']){
        $pass = "plese enter velid password and Confirm Password";
    }
    if (empty($nameErr)&&empty($emailErr)&&empty($passwordErr)&&empty($confirm_passwordErr)&&empty($messageErr)&&empty($pass)) {
            
            $name = $_POST['name'];
            $email = $_POST['email'];
            $password = $_POST['password'];
            $confirm_password = $_POST['confirm_password'];
            $message = $_POST['message'];

            $servername = "localhost";
            $username = "root";
            $password_db = "root";
            $dbname = "db_php";

            $conn = mysqli_connect($servername,$username,$password_db,$dbname);

            if (!$conn) {
                die("connection failed:".mysqli_connect_error());
            }

            $sql = "INSERT INTO users (name,email,password,confirm_password,message)
            VALUES('$name','$email','$password','$confirm_password','$message')"; 

            if ($conn->query($sql)===TRUE) {
                echo "<div class='msgbox alert alert-success p-1 text-center m-0 ' style='background-color:#9bffbbfa; width: 300px; height: 30px'>Data Insert Successfully</div>";
            }else{
                echo "Error: " .$sql . "<br>" . mysqli_error($conn);
            }
            $conn->close();
    }
}
?>
    <div class="container mt-5 ">
        <div class="card">
            <div class="card-header bg-dark text-white text-center">
                <h3>How to Create PHP 8 Contact Form With MySQL? - Mywebtuts.com</h3>
            </div>
            <div class="card-body">
                <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
                    
                    <div class="row">
                        <div class="col-md-6">
                            <label for="name">Name :</label>
                            <span class="error">*</span>
                            <input type="text" name="name" id="name" class="form-control">
                            <span class="error"><?php echo $nameErr ?></span>
                        </div>
                        <div class="col-md-6">
                            <label for="email">Email :</label>
                            <span class="error">*</span>
                            <input type="text" name="email" id="email" class="form-control">
                            <span class="error"><?php echo $emailErr ?></span>
                        </div>
                    </div>
                    
                    <div class="row mt-3">
                        <div class="col-md-6">
                            <label for="password">Password :</label>
                            <span class="error">*</span>
                            <input type="password" name="password" id="password" class="form-control">
                            <span class="error"><?php echo $passwordErr ?></span>
                        </div>
                        <div class="col-md-6">
                            <label for="confirm_password">Confirm Password :</label>
                            <span class="error">*</span>
                            <input type="password" name="confirm_password" id="confirm_password" class="form-control">
                            <span class="error"><?php echo $confirm_passwordErr ?></span>
                        </div>
                        <span class="error"><?php echo $pass ?></span>
                    </div>

                    <div class="mt-3">
                        <label for="message">Message :</label>
                        <span class="error">*</span>
                        <textarea rows="5" cols="40" class="form-control" id="message" name="message"></textarea>
                        <span class="error"><?php echo $messageErr ?></span>
                    </div>
                    <div class="text-center">
                            <input type="submit" name="save" class="btn btn-primary mt-3">
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
Output:

It will help you...
#PHP 8