How can PHP be used to prevent duplicate entries in a database?

To prevent duplicate entries in a database using PHP, you can first query the database to check if the entry already exists before inserting a new record. This can be done by selecting records that match the new entry's data and checking if any results are returned. If a match is found, the new entry should not be inserted to avoid duplicates.

// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check if the entry already exists
$query = "SELECT * FROM table WHERE column = 'value'";
$result = $mysqli->query($query);

if($result->num_rows == 0) {
    // Insert the new entry
    $insertQuery = "INSERT INTO table (column) VALUES ('value')";
    $mysqli->query($insertQuery);
    echo "New entry inserted successfully.";
} else {
    echo "Entry already exists.";
}

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