How can PHP be used to display a warning message when entering duplicate data in a database?

When entering data into a database, it is important to check for duplicates to maintain data integrity. In PHP, you can query the database to check if the data already exists before inserting it. If a duplicate is found, you can display a warning message to the user.

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

// Check for duplicate data
$query = "SELECT * FROM table WHERE column = 'value'";
$result = $conn->query($query);

if ($result->num_rows > 0) {
    echo "Warning: Data already exists in the database.";
} else {
    // Insert data into the database
    $insert_query = "INSERT INTO table (column) VALUES ('value')";
    $conn->query($insert_query);
    echo "Data inserted successfully.";
}

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