Delete query for 2 table is not working
public function deleteCustomerFunc($CustID)
{
$sql = "DELETE customer, cargo
FROM customer AS cust, cargo AS car
WHERE cust.CustID = car.CustID
AND CustID='$CustID'";
$result = mysql_query($sql);
return $result;
}
I have 2 table :
1) Cust开发者_如何学运维omer -CustID -Primary key 2)Cargo - id -primary key,CustID=Foreign key
A specific error message would help. But, my initial guess is that your AND clause is ambiguous - CustID can refer to either the cust or car table. While it doesn't matter which one in this case, the interpreter doesn't know that and you need to specify one of them.
public function deleteCustomerFunc($CustID)
{
$sql = "DELETE customer, cargo
FROM customer AS cust, cargo AS car
WHERE cust.CustID = car.CustID
AND cust.CustID='$CustID'";
$result = mysql_query($sql);
return $result;
}
First, DELETE syntax does not contains the fields:
DELETE FROM [tablename] WHERE [condition]
Second, you cannot delete from two tables in one expression, only one at time.
Maybe you should consider setting this up to cascade delete. Then when you delete from the first table it will automatically delete from the second via the foreign key.
How do I use on delete cascade in mysql?
精彩评论