這裏是SQL錯誤當我點擊刪除它會顯示這個錯誤?
SQL error: Cannot delete or update a parent row: a foreign key constraint fails (`motioncenter`.`news`, CONSTRAINT `news_ibfk_2` FOREIGN KEY (`fk_categories_id`) REFERENCES `categories` (`category_id`))
Line: 36
Fil: C:\Xampp\htdocs\xampp\site\admin\page_functions\category_delete.php
herre是category_delete.php文件 線:36回聲format_error_message(mysqli_error($ database_link),$查詢,LINE,FILE);
<?php
if (!isset($database_link))
{
die(header('location: index.php?page=categories'));
}
if (!isset($_GET['category_id']))
{
die(header('location: index.php?page=categories'));
}
$category_id = ($_GET['category_id'] * 1);
$query = "DELETE FROM categories WHERE category_id = $category_id";
if (mysqli_query($database_link, $query))
{
$_SESSION['message'] .= 'deleted<br />';
die(header('location: index.php?page=categories'));
}
else
{
echo format_error_message(mysqli_error($database_link), $query, __LINE__, __FILE__);
}
?>
問題是在數據庫表中的外鍵不在php代碼中。所以請提供您的表格結構。 –
你的分類表包含外鍵。因此,在刪除類別記錄之前,您必須刪除所有外部數據。 – Yami
類別表中有3 category_id - category_title - category_description – user3226978