can't update a foreign key but i can delete it already set on CASCADE for both

231 Views Asked by At

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?

0

There are 0 best solutions below