可以更新外键,但我可以将其删除在两个级联上

发布于 2025-02-05 08:18:53 字数 2281 浏览 3 评论 0原文

我尝试了为此找到的所有解决方案,而其中的无效。

要解释:我正在尝试在表classroom中更新主键code_class。此键也是预订表中的外键。

我保留此错误:

警告:pdostatement :: execute():sqlstate [23000]:完整性 约束违规:1451无法删除或更新父行: 外键约束失败(ebooking保留,约束 recervation_ibfk_3外键(code_class)参考classroomcode_class)在delete Cascade上的cascade上)in c:\ wamp64 \ www \ last \ ebooking2 \ profile \ admin \ admin \ updateclass.php on Line 38

但是我可以删除相同的密钥。我想提一下,正如错误所说,它们都设置在Cascade上。

这是我的更新PHP代码

$requete = $bdd->prepare('SELECT * FROM classroom Where code_class = :C ');
$requete->execute(array('C'=> $CR));
          while ($donnees = $requete->fetch()){
            if (isset($_POST) & !empty($_POST)) {
                $data = [
                    'name' => $_POST['Code'],
                    'surname' => $_POST['STIME'],
                    'floor' => $_POST['fTIME'],
                    'roomtp' => $_POST['ETIME'],
                    'hard'=> $_POST['HARD'],
                    'id' => $CR,
                ];
                $sql = "UPDATE classroom SET code_class=:name,capacity=:surname,floor=:floor,room_type=:roomtp,
                hardware_type=:hard WHERE code_class=:id ";
                $stmt= $bdd->prepare($sql);
                $stmt->execute($data);
                // echo "<script>window.location.assign('hardware.php')</script>";
            }
        

和我的删除代码,

<?php
// error_reporting(0);
session_start();
include('../../connexionDB.php');
if(!isset($_SESSION['CR']) ||$_SESSION['CR'] == ""){
   echo 'no id found';
}else {
    $AS = $_SESSION['CR'];
  
}
echo $AS;
$sql = "DELETE FROM `classroom` WHERE code_class =:c ";
    $statement = $bdd->prepare($sql);
$statement->bindParam('c', $AS);

// execute the statement
if ($statement->execute()) {
    echo 'publisher id  was deleted successfully.';
 
      }
?>

ALTER TABLE `reservation` ADD FOREIGN KEY (`code_class`) REFERENCES `classroom`(`code_class`) ON DELETE CASCADE ON UPDATE CASCADE;

看不到问题 - 有人可以帮忙吗?

I tried every solution I can find for this and non of them worked.

To explain: I'm trying to update a primary key code_class in the table classroom. This key is also a foreign key in the reservation table.

I keep gettimg this error:

Warning: PDOStatement::execute(): SQLSTATE[23000]: Integrity
constraint violation: 1451 Cannot delete or update a parent row: a
foreign key constraint fails (ebooking.reservation, CONSTRAINT
reservation_ibfk_3 FOREIGN KEY (code_class) REFERENCES classroom
(code_class) ON DELETE CASCADE ON UPDATE CASCADE) in
C:\wamp64\www\last\ebooking2\profile\admin\updateClass.php on line
38

But I can delete the same key. I would like to mention that they are both set on CASCADE, as the error says.

here is my Update php code

$requete = $bdd->prepare('SELECT * FROM classroom Where code_class = :C ');
$requete->execute(array('C'=> $CR));
          while ($donnees = $requete->fetch()){
            if (isset($_POST) & !empty($_POST)) {
                $data = [
                    'name' => $_POST['Code'],
                    'surname' => $_POST['STIME'],
                    'floor' => $_POST['fTIME'],
                    'roomtp' => $_POST['ETIME'],
                    'hard'=> $_POST['HARD'],
                    'id' => $CR,
                ];
                $sql = "UPDATE classroom SET code_class=:name,capacity=:surname,floor=:floor,room_type=:roomtp,
                hardware_type=:hard WHERE code_class=:id ";
                $stmt= $bdd->prepare($sql);
                $stmt->execute($data);
                // echo "<script>window.location.assign('hardware.php')</script>";
            }
        

and my delete code

<?php
// error_reporting(0);
session_start();
include('../../connexionDB.php');
if(!isset($_SESSION['CR']) ||$_SESSION['CR'] == ""){
   echo 'no id found';
}else {
    $AS = $_SESSION['CR'];
  
}
echo $AS;
$sql = "DELETE FROM `classroom` WHERE code_class =:c ";
    $statement = $bdd->prepare($sql);
$statement->bindParam('c', $AS);

// execute the statement
if ($statement->execute()) {
    echo 'publisher id  was deleted successfully.';
 
      }
?>

the key

ALTER TABLE `reservation` ADD FOREIGN KEY (`code_class`) REFERENCES `classroom`(`code_class`) ON DELETE CASCADE ON UPDATE CASCADE;

I can't see the problem - can someone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文