What are the best practices for handling form data sent via POST in PHP to avoid duplication?

When handling form data sent via POST in PHP, to avoid duplication, it's best practice to check if the data already exists in the database before inserting it. This can be done by querying the database with the submitted data to see if a matching record already exists. If a match is found, you can choose to either update the existing record or display an error message to the user.

// Assuming $conn is your database connection

// Check if the form data already exists in the database
$stmt = $conn->prepare("SELECT * FROM your_table WHERE column_name = ?");
$stmt->bind_param("s", $_POST['form_field']);
$stmt->execute();
$result = $stmt->get_result();

if($result->num_rows > 0) {
    // Data already exists, handle accordingly (update record or display error message)
} else {
    // Data does not exist, proceed with inserting the form data into the database
}