2010-10-02 8 views
1

pourquoi est-il la première fois que je modifier une clé forein il me permet d'éditer sa valeur, puis après la seconde essayer soulève une erreur comme celleédition clé étrangère

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) 

vous pouvez voir ma requête d'édition

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> 
<? } 
+0

Publiez également ce que vous essayez de faire non seulement le résultat. – frisco

+0

Si vous pouviez poster votre code afin que je puisse construire votre base de données, je continuerais volontiers à vous aider. –

+0

je l'ai dans une autre question. il est complet avec ma requête, la structure de la table des produits et la page d'affichage. Tu peux le vérifier ici. Merci de votre aide. voici le lien http://stackoverflow.com/questions/3845460/edit-query-error-with-foreign-key-constraints –

Répondre

2
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) 

Ce message vous dit que vous ne pouvez pas effectuer l'opération que vous essayez d'effectuer parce que, il est en violation de la contrainte de clé étrangère.

Vous devez évaluer votre requête pour vérifier que vous n'êtes pas:

(a) l'ajout d'une nouvelle valeur à la table de clé étrangère avant de l'utiliser dans sadsystem/products (qui fait également référence à l'exécution d'une insertion, mise à jour, ou supprimer). (B) en essayant de supprimer une valeur de la table de clé étrangère alors qu'elle est liée à une autre table qui repose sur la valeur.

(c) un autre problème obscur.

+0

vous pouvez voir ma requête d'édition je suppose que je manque quelque chose –