Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php bindParam不';外键约束设置为“级联”时不工作_Php_Mysql_Pdo_Cascade_Bindparam - Fatal编程技术网

Php bindParam不';外键约束设置为“级联”时不工作

Php bindParam不';外键约束设置为“级联”时不工作,php,mysql,pdo,cascade,bindparam,Php,Mysql,Pdo,Cascade,Bindparam,我正在为一个任务创建一个论坛站点,代码运行良好,直到我更改了两件事:我将数据库约束更改为级联(需要与站点其余部分类似),并将查询函数更改为准备、绑定和执行以确保安全 此代码有效 $insert_post_query = "INSERT INTO posts ( thread_id, user_id, username, post_content, post_date ) VALUES ( '$topic_id', '$user_id', '$username', '$post_c

我正在为一个任务创建一个论坛站点,代码运行良好,直到我更改了两件事:我将数据库约束更改为级联(需要与站点其余部分类似),并将查询函数更改为准备、绑定和执行以确保安全

此代码有效

$insert_post_query =
"INSERT INTO posts (
    thread_id, user_id, username, post_content, post_date
)
VALUES (
    '$topic_id', '$user_id', '$username', '$post_content', '$post_date'
)";
$post_result = $db->query($insert_post_query);
此代码不起作用

$insert_post_query =
"INSERT INTO posts (
    thread_id, user_id, username, post_content, post_date
)
VALUES (
    '?', '?', '?', '?', '?'
)";
try{
$post_result = $db->prepare($insert_post_query);
$post_result->bindParam(1,$topic_id,PDO::PARAM_INT);
$post_result->bindParam(2,$user_id,PDO::PARAM_INT);
$post_result->bindParam(3,$username,PDO::PARAM_STR);
$post_result->bindParam(4,$post_content,PDO::PARAM_STR);
$post_result->bindParam(5,$post_date,PDO::PARAM_STR);
$post_result->execute();

}catch(Exception $e){
    echo "Unable to add the post<br>$e";
    exit;
}
$insert\u post\u查询=
“在柱子上插入(
线程id、用户id、用户名、发布内容、发布日期
)
价值观(
'?', '?', '?', '?', '?'
)";
试一试{
$post\u result=$db->prepare($insert\u post\u query);
$post_result->bindParam(1,$topic_id,PDO::PARAM_INT);
$post_result->bindParam(2,$user_id,PDO::PARAM_INT);
$post_result->bindParam(3,$username,PDO::PARAM_STR);
$post_result->bindParam(4,$post_content,PDO::PARAM_STR);
$post_result->bindParam(5,$post_date,PDO::PARAM_STR);
$post_结果->执行();
}捕获(例外$e){
echo“无法添加帖子
$e”; 出口 }
这是错误

PDOException:SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(
forum
posts
,约束
posts\u ibfk\u 1
外键(
thread\u id
)引用
threads
thread\u id
)在更新级联上删除级联)


我对PHP相当陌生,所以它可能很简单,任何帮助都将不胜感激。

您可以这样做,在插入数据集foreign\u key\u checks=0之前,使用值为1的同一查询行再次激活它

SET foreign_key_checks = 0;
SET foreign_key_checks = 1;

$insert_post_query =
"INSERT INTO posts (
    thread_id, user_id, username, post_content, post_date
)
VALUES (
    '?', '?', '?', '?', '?'
)";
try{
$post_result = $db->prepare("SET foreign_key_checks = 0");
$post_result = $db->prepare($insert_post_query);
$post_result->bindParam(1,$topic_id,PDO::PARAM_INT);
$post_result->bindParam(2,$user_id,PDO::PARAM_INT);
$post_result->bindParam(3,$username,PDO::PARAM_STR);
$post_result->bindParam(4,$post_content,PDO::PARAM_STR);
$post_result->bindParam(5,$post_date,PDO::PARAM_STR);
$post_result->execute();
}catch(Exception $e){
    echo "Unable to add the post<br>$e";
    exit;
}
$post_result = $db->prepare("SET foreign_key_checks = 1");
设置外键检查=0;
设置外键检查=1;
$insert\u post\u查询=
“在柱子上插入(
线程id、用户id、用户名、发布内容、发布日期
)
价值观(
'?', '?', '?', '?', '?'
)";
试一试{
$post_result=$db->prepare(“设置外键检查=0”);
$post\u result=$db->prepare($insert\u post\u query);
$post_result->bindParam(1,$topic_id,PDO::PARAM_INT);
$post_result->bindParam(2,$user_id,PDO::PARAM_INT);
$post_result->bindParam(3,$username,PDO::PARAM_STR);
$post_result->bindParam(4,$post_content,PDO::PARAM_STR);
$post_result->bindParam(5,$post_date,PDO::PARAM_STR);
$post_结果->执行();
}捕获(例外$e){
echo“无法添加帖子
$e”; 出口 } $post_result=$db->prepare(“设置外键检查=1”);
您可以这样做,在插入数据集foreign\u key\u checks=0之前,使用值为1的同一查询行再次激活它

SET foreign_key_checks = 0;
SET foreign_key_checks = 1;

$insert_post_query =
"INSERT INTO posts (
    thread_id, user_id, username, post_content, post_date
)
VALUES (
    '?', '?', '?', '?', '?'
)";
try{
$post_result = $db->prepare("SET foreign_key_checks = 0");
$post_result = $db->prepare($insert_post_query);
$post_result->bindParam(1,$topic_id,PDO::PARAM_INT);
$post_result->bindParam(2,$user_id,PDO::PARAM_INT);
$post_result->bindParam(3,$username,PDO::PARAM_STR);
$post_result->bindParam(4,$post_content,PDO::PARAM_STR);
$post_result->bindParam(5,$post_date,PDO::PARAM_STR);
$post_result->execute();
}catch(Exception $e){
    echo "Unable to add the post<br>$e";
    exit;
}
$post_result = $db->prepare("SET foreign_key_checks = 1");
设置外键检查=0;
设置外键检查=1;
$insert\u post\u查询=
“在柱子上插入(
线程id、用户id、用户名、发布内容、发布日期
)
价值观(
'?', '?', '?', '?', '?'
)";
试一试{
$post_result=$db->prepare(“设置外键检查=0”);
$post\u result=$db->prepare($insert\u post\u query);
$post_result->bindParam(1,$topic_id,PDO::PARAM_INT);
$post_result->bindParam(2,$user_id,PDO::PARAM_INT);
$post_result->bindParam(3,$username,PDO::PARAM_STR);
$post_result->bindParam(4,$post_content,PDO::PARAM_STR);
$post_result->bindParam(5,$post_date,PDO::PARAM_STR);
$post_结果->执行();
}捕获(例外$e){
echo“无法添加帖子
$e”; 出口 } $post_result=$db->prepare(“设置外键检查=1”);
您是否有
线程
实体,其
id
等于您在bindParam中提供的
线程id
?删除查询占位符周围的撇号。是否有
线程
实体,其
id
等于您在bindParam中提供的
线程id
?删除查询占位符周围的撇号。