查询错误:无法删除或更新父行:外键约束失败

时间:2018-05-24 19:31:24

标签: php phpmyadmin

我正在尝试删除与某个用户ID关联的3个表中的内容,并且出现以下错误:

Error in query: Cannot delete or update a parent row: a foreign key constraint fails (`MyName_4.2c`.`tbl_reservation`, CONSTRAINT `tbl_reservation_ibfk_2` FOREIGN KEY (`propertyId`) REFERENCES `tbl_property` (`propertyId`)) 

代码:

<?php
    session_start();
    $userId = $_GET['userId'];
    require_once('databaseConn.php');


    $query = "DELETE FROM tbl_reservation WHERE userId = '$userId'";
    $result = mysqli_query($connection, $query)
            or die("Error in query: ". mysqli_error($connection));

    $query2 = "DELETE FROM tbl_property WHERE userId = '$userId'";
    $result2 = mysqli_query($connection, $query2)
            or die("Error in query: ". mysqli_error($connection));

    $query3 = "DELETE FROM tbl_users WHERE userId = '$userId'";
    $result3 = mysqli_query($connection, $query3)
            or die("Error in query: ". mysqli_error($connection));

    header('Location: index.php');
?>       

我的表格:

CREATE TABLE `tbl_property` (
`propertyId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`capacity` int(11) NOT NULL,
`pricePerNight` double NOT NULL,
`locationId` int(11) NOT NULL,
`image` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `tbl_reservation` (
`reservationId` int(11) NOT NULL,
`propertyId` int(11) NOT NULL,
`date_from` date NOT NULL,
`date_to` date NOT NULL,
`amountPaid` double NOT NULL,
`userId` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE `tbl_users` (
`userId` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`surname` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE `tbl_location` (
`locationId` int(11) NOT NULL,
`location` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

0 个答案:

没有答案
相关问题