To create a contact form in PHP and join it to a MySQL database, observe those steps:
Step 1: Set up the MySQL database
First, create a MySQL database and table to store the touch shape statistics. You can use the subsequent SQL query:
CREATE DATABASE contact_form_db;
USE contact_form_db;
CREATE TABLE contact_messages (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
submission_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 2: Create the HTML form
Create an HTML file for the contact form. Save it as “contact_form.html”:
<label for="email">Email:</label> <input type="email" id="email" name="email" required><br> <label for="message">Message:</label> <textarea id="message" name="message" required></textarea><br> <input type="submit" value="Submit"> </form>
Step 3: Create the PHP script to handle form submission
Create a PHP document known as “submit_contact_form.Php” to handle the form submission, connect with the MySQL database, and insert the data:
<?php
// Database credentials
$servername = “localhost”;
$username = “your_username”;
$password = “your_password”;
$dbname = “contact_form_db”;
// Create a connection to the database
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) { die(“Connection failed: ” . $conn->connect_error); }
// Get the form data
$name = $_POST[‘name’];
$email = $_POST[’email’];
$message = $_POST[‘message’];
// Insert the data into the database
$sql = “INSERT INTO contact_messages (name, email, message) VALUES (?, ?, ?)”;
$stmt = $conn->prepare($sql);
$stmt->bind_param(“sss”, $name, $email, $message);
if ($stmt->execute()) { echo “Your message has been submitted successfully!”; } else { echo “Error: ” . $sql . “<br>” . $conn->error; } // Close the connection $stmt->close(); $conn->close(); ?>Make sure to replace “your_username” and “your_password” along with your real MySQL username and password.
Now, whilst you fill out the contact form and post it, the shape records could be stored inside the MySQL database.
Remember to constantly validate and sanitize consumer enter to keep away from protection vulnerabilities, along with SQL injection assaults.