0
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) Customer -CustID -Primary key 2)Cargo - id -primary key,CustID=Foreign key

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Joe
  • 17
  • 1
  • 7
  • Do you get any SQL error message? – Shi Aug 01 '11 at 01:24
  • 1
    using this type of non-standard syntax is not a great idea. Things like this make code difficult to maintain and impossible to transition to a different dbms in the future – Bueller Aug 01 '11 at 01:35

3 Answers3

4

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;
}
Derek
  • 21,828
  • 7
  • 53
  • 61
0

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.

Alex_L
  • 2,658
  • 1
  • 15
  • 13
0

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?

Community
  • 1
  • 1
Taryn
  • 242,637
  • 56
  • 362
  • 405