I have 3 tables which is subcategory, product, and prod_category.
When I create my product, if it successful, it will return the successfull product_id, after that it will capture the form subcategory column ,to carry out the next query which insert product_id and subcategory_id to the prod_category table. I tested out in a single php file to test the whether the function is working. It can insert, but when I fit into my situation, it was giving me this error.
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
yokotachi.prod_category, CONSTRAINTprod_category_ibfk_4FOREIGN KEY (product_id) REFERENCESproduct(product_id) ON DELETE CASCADE ON UPDATE CASCADE) in C:\xampp\htdocs\yokotachi\models\Prod_category.php on line 27
I confirm that, 2 parameter was passed into the function, but i dont know why it cannot perform the query.
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");
}}
i did saw 1 post is reindex problem, and asked to truncate the table. But I have no idea , how to perform the truncate query in my situation.
Thanks in advance.
The error message seems to be telling you that the product ID number you’re trying to use in the table “prod_category” doesn’t exist in the table “product”.
You need to find out what value $executed_product_id has after this line executes.
If the value it has isn’t in the table “product”, then that’s your problem. The foreign key reference requires that product ID to exist in “product” before you can use it in “prod_category”.