How can duplicate entries in a database be prevented using PHP?

Duplicate entries in a database can be prevented by checking if the entry already exists before inserting it. This can be done by querying the database to see if a record with the same values already exists. If a duplicate entry is found, the new entry should not be inserted.

// Connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);

// Check if the entry already exists
$query = "SELECT * FROM table_name WHERE column_name = 'value'";
$result = $conn->query($query);

if ($result->num_rows == 0) {
    // Insert the new entry
    $insert_query = "INSERT INTO table_name (column_name) VALUES ('value')";
    $conn->query($insert_query);
} else {
    echo "Duplicate entry found!";
}

// Close the database connection
$conn->close();