tags:

views:

38

answers:

1

why is it the first time i edit a forein key it allows me to edit its value and then after the second try it brings up an error like this

Cannot add or update a child row: a foreign key constraint fails (`sadsystem/products`, CONSTRAINT `products_ibfk_3` FOREIGN KEY (`size_id`) REFERENCES `product_sizes` (`size_id`) ON DELETE CASCADE ON UPDATE CASCADE)

you can see my edit query

if($mode=="editproduct") 
{
$product_id=$_GET["product_id"];
$sql="SELECT * FROM products WHERE product_id='$product_id'";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
$product_id=$row['product_id'];
$product_name=$row['product_name'];
$product_color=$row['product_color'];
$size_id=$row['size_id'];
$product_description=$row['product_description'];
$brand_id=$row['brand_id'];
$category_id=$row['category_id'];
$supplier_id=$row['supplier_id'];
$product_standardPrice=$row['product_standardPrice'];
$product_unitPrice=$row['product_unitPrice'];
} ?>
<link href="default.css" rel="stylesheet" type="text/css">
<form method="post" action="forms.php">
<table align="center">
<tr>
<td><strong>Edit Product</strong></td>
<td><input type="hidden" name="product_id" value="<? echo $product_id ;?>" /></td>
</tr>
<tr>
<td>Product Name</td>
<td><input type="text" name="product_name" value="<? echo $product_name ;?>" /></td>
</tr>
<tr>
<td>Color</td>
<td><input type="text" name="product_color" value="<? echo $product_color ;?>" /></td>
</tr>
<tr>
<td>Size</td>
<td>
<?
    $query="SELECT * FROM product_sizes ORDER BY size_id ASC";
    $result = mysql_query ($query);
    echo "<select name=size_id>";
    while($nt=mysql_fetch_array($result))
    {
    echo "<option value=$nt[$size_id]>$nt[size_name]</option>";
    }
    echo "</select>";
?>
</td>
</tr>
<tr>
<td>Description</td>
<td><input type="text" name="product_description" value="<? echo $product_description ;?>" /></td>
</tr>
<tr>
<td>Brand</td>
<td>
<?
    $query="SELECT * FROM brands ORDER BY brand_name ASC";
    $result = mysql_query ($query);
    echo "<select name=brand_id>";
    while($nt=mysql_fetch_array($result))
    {
    echo "<option value=$nt[brand_id]>$nt[brand_name]</option>";
    }
    echo "</select>";
?>
</td>
</tr>
<tr>
<td>Category</td>
<td>
<?
    $query="SELECT * FROM categories ORDER BY category_name ASC";
    $result = mysql_query ($query);
    echo "<select name=category_id>";
    while($nt=mysql_fetch_array($result))
    {
    echo "<option value=$nt[category_id]>$nt[category_name]</option>";
    }
    echo "</select>";
?>
</td>
</tr>
<tr>
<td>Supplier</td>
<td>
<?
    $query="SELECT * FROM suppliers ORDER BY supplier_name ASC";
    $result = mysql_query ($query);
    echo "<select name=supplier_id>";
    while($nt=mysql_fetch_array($result))
    {
    echo "<option value=$nt[supplier_id]>$nt[supplier_name]</option>";
    }
    echo "</select>";
?>
</td>
</tr>
<tr>
<td>Standard Price</td>
<td><input type="text" name="product_standardPrice" value="<? echo $product_standardPrice ;?>"/></td>
</tr>
<tr>
<td>Unit Price</td>
<td><input type="text" name="product_unitPrice" value="<? echo $product_unitPrice ;?>" /></td>
</tr>
<tr>
<td><input type="submit" name="editproduct" value="Save" /></td>
</tr>
</table>
</form>
<? }
+2  A: 
Cannot add or update a child row: a foreign key constraint fails (`sadsystem/products`, CONSTRAINT `products_ibfk_3` FOREIGN KEY (`size_id`) REFERENCES `product_sizes` (`size_id`) ON DELETE CASCADE ON UPDATE CASCADE)

This message is telling you that you cannot perform the operation you are trying to perform because, it is in violation of the foreign key constraint.

You need to evaluate your query to verify that you are not:

(a) adding a new value to the foreign key table prior to using it in sadsystem/products ( which also refers to performing an insert, update, or delete).

(b) trying to delete a value from the foreign key table while it is linked to another table that relies on the value.

(c) some other obscure issue.

Michael Eakins
you can see my edit query i guess im missing something
kester martinez