gpt4 book ai didi

PHP PDO 准备删除。为什么会失败?

转载 作者:行者123 更新时间:2023-11-29 03:52:58 25 4
gpt4 key购买 nike

<分区>

我尝试通过 ajax 响应执行一小段代码。我已经通过了 ID,但由于某种原因,我的删除语句失败了(不删除记录,因此添加到 $err 数组)。我敢肯定这是愚蠢的事情,但它现在不会跳到我这里来。

PHP 代码

<?php
define('INCLUDE_CHECK',true);

require '../../connect.php';
require '../../functions.php';
// Those two files can be included only if INCLUDE_CHECK is defined
// sets site to require https
echo redirectToHTTPS();
session_name('tzLogin');
session_set_cookie_params(2*7*24*60*60);
session_start();



if (isset($_POST['id']) && $_SESSION['id'] && $_SESSION['permlvl']==3 )
{
$id = is_numeric($_POST['id']);
$err = array();
$query = "DELETE FROM employees WHERE id = :id";
$statement = $db -> prepare($query);
$statement -> BindParam('id', $id, PDO::PARAM_INT);
$result = $statement -> execute();
$statement -> closecursor();
if ($result === true){
}
else{
$err[] = "error";
}
}
//check for error messages
if(!count($err))
{
echo 'success';
}
else{
//on failure, future will include logging either by sending email or writing to a log file
}
?>

更新我已经更改了数据库错误模式并且可以显示它。所以它必须与我的数据库的设计方式有关。

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (login.thrives, CONSTRAINT thrives_ibfk_1 FOREIGN KEY (n_emp) REFERENCES employees (ID))' in /Applications/XAMPP/xamppfiles/htdocs/jen/maintabs/Employees/delete.php:23 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/jen/maintabs/Employees/delete.php(23): PDOStatement->execute() #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/jen/maintabs/Employees/delete.php on line 23

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com