How can PHP be utilized to limit form submissions to once per IP address using a database?

To limit form submissions to once per IP address using a database, we can create a table in the database to store IP addresses that have already submitted the form. When a form is submitted, we can check if the IP address is already in the database. If it is, we can prevent the form submission. If it is not, we can allow the submission and add the IP address to the database.

<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get the IP address of the user
$ip_address = $_SERVER['REMOTE_ADDR'];

// Check if the IP address is already in the database
$sql = "SELECT * FROM form_submissions WHERE ip_address = '$ip_address'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "You have already submitted the form.";
} else {
    // Insert the IP address into the database
    $sql = "INSERT INTO form_submissions (ip_address) VALUES ('$ip_address')";
    if ($conn->query($sql) === TRUE) {
        echo "Form submitted successfully.";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

$conn->close();
?>