How can PHP developers ensure data integrity and consistency when linking articles to categories in a marketplace application?
To ensure data integrity and consistency when linking articles to categories in a marketplace application, PHP developers can implement foreign key constraints in their database schema. This will enforce referential integrity between the articles and categories tables, ensuring that only valid category IDs can be linked to articles.
// Add foreign key constraint to articles table
$sql = "ALTER TABLE articles
ADD CONSTRAINT fk_category_id
FOREIGN KEY (category_id)
REFERENCES categories(id)
ON DELETE CASCADE";
// Execute the SQL query
mysqli_query($conn, $sql);
Related Questions
- How can PHP developers ensure that the points system is efficiently managed and updated?
- Is it possible to create a web robot in PHP that can navigate websites, click on links, and submit forms?
- In the given examples, what are the different ways to correctly echo a PHP variable inside JavaScript code?