我有3个表是子类别,产品和prod_category。完整性约束违反PDO方法(创建查询)php
当我创建我的产品时,如果成功,它将返回成功的product_id,之后它将捕获表单子类别列,以执行下一个将product_id和subcategory_id插入prod_category表的查询。我测试了一个单一的PHP文件来测试功能是否正常工作。它可以插入,但是当我适合我的情况时,它给了我这个错误。
警告:PDOStatement对象::执行()[pdostatement.execute]:SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键约束失败(
yokotachi
prod_category
,约束prod_category_ibfk_4
。外键(product_id
)参考文献product
(product_id
)ON DELETE CASCADE ON UPDATE CASCADE)在C:\ XAMPP \ htdocs中\ yokotachi \模型\ Prod_category.php第27行
本人确认,2参数被传递到该函数,但我不知道为什么它不能执行查询。
if($_POST){
$title = mysql_real_escape_string($_POST['title']);
$model = mysql_real_escape_string($_POST['model']);
$description = stripslashes($_POST['description']);
$feature = stripslashes($_POST['feature']);
$specification = stripslashes($_POST['specification']);
$subcategory_name = mysql_real_escape_string($_POST['product_subcategory']);
$sub_obj = $subcategory->find_by_sub_category_name($subcategory_name);
$subcategory_id = $sub_obj->sub_category_id;
echo $subcategory_id;
$executed_product_id=
$product->create($title,$model,$description,$feature,$specification);
echo "im here".$subcategory_id;
echo $executed_product_id;
$responde = $prod_category->create($executed_product_id,$subcategory_id);
if($responde){
echo "<script>";
echo "alert(".$executed_product_id.");";
echo "</script>";
header("location: ../../../views/admin/product/product_index.php");
}}
我看到1帖子是reindex问题,并要求截断表格。但我不知道如何在我的情况下执行截断查询。
在此先感谢。
你是设置数据库表的人还是别人的工作? –
@ N.B .:我会说别人设置了表格。致OP:阅读_ [FOREIGN KEY CONSTRAINTS](http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html)_,并且为了避免必须做3个单独的查询:_ [JOIN](http://dev.mysql.com/doc/refman/5.0/en/join.html)_。 –
liine 27是什么? –