How can the issue of duplicate entries in a database table be avoided when marking tips as sent in a PHP application?

Duplicate entries in a database table can be avoided when marking tips as sent in a PHP application by checking if the entry already exists before inserting a new one. This can be done by querying the database to see if a tip with the same ID already exists. If it does, update the existing entry instead of inserting a new one.

// Check if tip with the same ID already exists in the database
$existing_tip = $pdo->query("SELECT * FROM tips WHERE id = $tip_id")->fetch();

if($existing_tip) {
    // Update existing tip entry
    $pdo->query("UPDATE tips SET sent = 1 WHERE id = $tip_id");
} else {
    // Insert new tip entry
    $pdo->query("INSERT INTO tips (id, sent) VALUES ($tip_id, 1)");
}