2012-10-17 47 views
2

我有3个表是子类别,产品和prod_category。完整性约束违反PDO方法(创建查询)php

当我创建我的产品时,如果成功,它将返回成功的product_id,之后它将捕获表单子类别列,以执行下一个将product_id和subcategory_id插入prod_category表的查询。我测试了一个单一的PHP文件来测试功能是否正常工作。它可以插入,但是当我适合我的情况时,它给了我这个错误。

警告:PDOStatement对象::执行()[pdostatement.execute]:SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键约束失败(yokotachiprod_category,约束prod_category_ibfk_4。外键(product_id)参考文献productproduct_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问题,并要求截断表格。但我不知道如何在我的情况下执行截断查询。

在此先感谢。

+0

你是设置数据库表的人还是别人的工作? –

+0

@ 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)_。 –

+0

liine 27是什么? –

回答

0

错误消息似乎告诉您,表“prod_category”中您尝试使用的产品ID号在表“product”中不存在。

您需要找出该行执行后$ execution_product_id具有的值。

$executed_product_id= 
    $product->create($title,$model,$description,$feature,$specification); 

如果它所具有的值不在表“product”中,那就是你的问题。在“prod_category”中使用它之前,外键引用要求产品ID存在于“产品”中。

+0

Oic,但实际上execution_product_id是product_id,它是返回时执行 $ product> create($ title,$ model,$ description,$ feature,$ specification); 我试图在执行查询之前回显,并且有一个已经存在的值。即使在执行函数之后,在准备语句之后和执行查询之前,我试图将其回显出来,并且它具有我想要的值。所以,我不知道,它怎么可能是这样的:( – Nich

+0

你是怎么回声?PHP变量?如果是这样,看看实际的表格。 –

+0

yup,怎么说看看实际表格?意思是你的意思是在执行create时,是否存在或创建了product_id? – Nich