What potential pitfalls can arise when using select statements in PHP to avoid duplicate entries in a database?

When using select statements in PHP to avoid duplicate entries in a database, a potential pitfall is that another process could insert a duplicate entry between the time you check for duplicates and the time you actually insert the new entry. To solve this issue, you can use database constraints, such as unique indexes, to enforce uniqueness at the database level.

// Check if the entry already exists in the database
$query = "SELECT * FROM your_table WHERE column_name = :value";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':value', $value);
$stmt->execute();
$result = $stmt->fetch();

if (!$result) {
    // Insert the new entry if it doesn't already exist
    $query = "INSERT INTO your_table (column_name) VALUES (:value)";
    $stmt = $pdo->prepare($query);
    $stmt->bindParam(':value', $value);
    $stmt->execute();
} else {
    echo "Entry already exists in the database";
}