【发布时间】:2012-10-17 15:04:22
【问题描述】:
我有 3 个表,分别是 subcategory、product 和 prod_category。
当我创建我的产品时,如果成功,它会返回成功的product_id,然后它会捕获表单子类别列,执行下一个查询,将product_id和subcategory_id插入prod_category表。我在单个 php 文件中进行了测试,以测试该功能是否正常工作。它可以插入,但是当我适应我的情况时,它给了我这个错误。
警告:PDOStatement::execute() [pdostatement.execute]: SQLSTATE[23000]: 完整性约束违规:1452 无法添加或更新子行:外键约束失败 (
yokotachi.prod_category, CONSTRAINTprod_category_ibfk_4FOREIGN KEY (product_id) REFERENCESproduct(product_id) ON DELETE CASCADE ON UPDATE CASCADE) 在 C:\xampp\htdocs\yokotachi\models\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 个帖子是重新索引问题,并要求截断表格。但我不知道如何在我的情况下执行截断查询。
提前致谢。
【问题讨论】:
-
数据库表是你建的还是别人的?
-
@N.B.:我会说其他人设置了表格。致 OP:阅读 FOREIGN KEY CONSTRAINTS 并且为了避免必须执行 3 个单独的查询:JOIN。
-
第 27 行是 if(!$stmt->execute()){throw new PDOException()}
-
@N.B 不,我正在自己布置桌子......
标签: php mysql pdo many-to-many