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

atr*_*nce 1 php mysql pdo prepared-statement

我有一小段代码,我试图通过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
    }
?>
Run Code Online (Sandbox Code Playgroud)

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

致命错误:未捕获异常'PDOException',消息'SQLSTATE [23000]:完整性约束违规:1451无法删除或更新父行:外键约束失败(login.thrives,CONSTRAINT thrives_ibfk_1FOREIGN KEY(n_emp)REFERENCES employees(ID))'/ Applications /XAMPP/xamppfiles/htdocs/jen/maintabs/Employees/delete.php:23堆栈跟踪:#0 /Applications/XAMPP/xamppfiles/htdocs/jen/maintabs/Employees/delete.php(23):PDOStatement-> execute( )第23/Applications/XAMPP/xamppfiles/htdocs/jen/maintabs/Employees/delete.php中引发的#1 {main}

dec*_*eze 6

$id = is_numeric($_POST['id']);

...

$statement -> BindParam('id', $id, PDO::PARAM_INT);
Run Code Online (Sandbox Code Playgroud)

is_numeric返回一个布尔值.值的值$idtruefalse,而不是数字.