Are there any best practices for managing form submissions and database inserts in PHP to prevent duplicate entries?

When managing form submissions and database inserts in PHP, it is important to prevent duplicate entries to maintain data integrity. One common approach to prevent duplicate entries is to check if the entry already exists in the database before inserting a new record. This can be done by querying the database with the submitted data and only inserting if no matching entry is found.

// Check if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    // Retrieve form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    
    // Check if the entry already exists in the database
    $query = "SELECT * FROM users WHERE name = '$name' AND email = '$email'";
    $result = mysqli_query($connection, $query);
    
    if(mysqli_num_rows($result) == 0) {
        // Insert new record into the database
        $insert_query = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
        mysqli_query($connection, $insert_query);
        echo "Record inserted successfully!";
    } else {
        echo "Duplicate entry found!";
    }
}