how to save data from html form to a database using php?

Today, We want to share with you php contact us form with database.In this post we will show you Creating simple PHP contact form, hear for php contact form send email we will give you demo and example for implement.In this post, we will learn about Create Contact Form And Send Email using PHP with an example.

PHP MySQL contact us form with validation using Bootstrap

Step 1: Create contactus table

contactus table

CREATE TABLE `contactus` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `message` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Step 2: DataBase Configuration File

config.php

<?php


define (DB_USER, "root");
define (DB_PASSWORD, "root");
define (DB_DATABASE, "sole");
define (DB_HOST, "localhost");


$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
?>

Step 3: Create index.php

index.php

<!DOCTYPE html>
<html>
<head>
    <title>PHP MySQL contact us form with validation using Bootstrap - www.pakainfo.com</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>


<div class="container">
    <h3>PHP MySQL contact us form with validation using Bootstrap</h3>
    <b>PHP Contact Form</b>
    <form action="data.php" method="POST">
        <div class="form-group">
            <label>Name:</label>
            <input type="text" name="name" class="form-control" required>
        </div>
        <div class="form-group">
            <label>Email:</label>
            <input type="email" name="email" class="form-control" required>
        </div>
        <div class="form-group">
            <label>Message:</label>
            <textarea class="form-control" name="message" required></textarea>
        </div>
        <div class="form-group">
            <button class="btn btn-success" type="submit">Submit</button>
        </div>
    </form>
</div>


</body>
</html>

Step 4: Create data.php

data.php

<?php

require('config.php');

extract($_POST);

$sql = "INSERT into contactus (name,email,message,created_date) VALUES('" . $name . "','" . $email . "','" . $message . "','" . date('Y-m-d') . "')";

$success = $mysqli->query($sql);

if (!$success) {
    die("Couldn't enter data: ".$mysqli->error);
}

echo "Thank You For Contacting Us ";

$conn->close();

?>

I hope you get an idea about PHP Contact Form.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.