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
?删除查询占位符周围的撇号。