开发者

simple update of foreign key in php mysql website

开发者 https://www.devze.com 2023-01-20 22:51 出处:网络
hi i cannot update a table which has foreign keys on it. in this table, instead of displaying the primary keys of the foreign key, i choose to display their names: this is a simple diagram:

hi i cannot update a table which has foreign keys on it. in this table, instead of displaying the primary keys of the foreign key, i choose to display their names: this is a simple diagram: Here are my foreign tables:

Size Table:

sId  sName
1    1x1
2    2x2

Brand Table:

bId  bName
1    brand1
2    brand2

Supplier Table:

sId  sName
1    supp1
2    supp2

So here is my Warehouse Table using a join statement:

pId  pName  pSize pBrand  pSupplier
1    prod1  1x1   brand1  supp1
2    prod2  2x2   brand2  supp2

here is my edit in php and mysql form:

########### EDIT PRODUCT
if(isset($_POST['editproduct'])){
$product_id=$_POST["product_code"];
$product_name=$_POST["product_name"];       
$size_name=$_POST["size_name"]; 
$brand_name=$_POST["brand_name"];       
$supplier_name=$_POST["supplier_name"];     
$sql = "UPDATE warehouse SET 
product_name='$product_name'
,size_id='$size_id'
,brand_id='$brand_id'
,supplier_id='$supplier_id'
WHERE 
product_code='$product_code'";
$result=mysql_query($sql,$connection) or die(mysql_error());
header("location: warehouse.php");
} ?>

the weird thing is that the first try i edit the table it does not error. but for the second time, it prompts me the error of foreign key constraint, :(

i 开发者_开发知识库have a feeling that because i use a join statement in my warehouse table, that conflicts my update query since the one i am updating is the primary key and i only display its name.


you reference the variables $size_id, $brand_id, $supplier_id in your SQL statement but never set them anywhere..

Though if you say that the edit works the first time, I'm guessing you might not have posted all your code?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号