What are common pitfalls when using foreign keys in PHP and MySQL databases?

One common pitfall when using foreign keys in PHP and MySQL databases is not properly setting up the foreign key constraints in the database schema. This can lead to data integrity issues and inconsistencies in the database. To solve this, make sure to define the foreign key constraints when creating the tables in the database.

// Create a table with foreign key constraint
$sql = "CREATE TABLE orders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    product_id INT,
    FOREIGN KEY (product_id) REFERENCES products(id)
)";

// Execute the SQL query
mysqli_query($conn, $sql);