-1

Suppose I have two tables in a database in MYSQL, namely

Customer_image

Customer_details

Now I want that the ID field in customer_details to show up as the UID field in customer_image table. And if I update or delete any record in any one of the tables, the related record should also be affected accordingly. If anyone could relate the fields through php code it would be great.

CinCout
  • 9,486
  • 12
  • 49
  • 67
aditya
  • 339
  • 2
  • 12
  • 3
    Possible duplicate of [Create Multiple MySQL tables using PHP](http://stackoverflow.com/questions/14023810/create-multiple-mysql-tables-using-php) – Ala Alam Falaki Mar 26 '16 at 08:39

1 Answers1

0

Firstly, you should do it with MySQL's ON DELETE, ON UPDATE methods.http://www.mysqltutorial.org/mysql-on-delete-cascade/

But if you really want to do it with PHP for some reason, create an array and store foreign key relations data in this array like this:

[ 'customer_details' => 'id' => ['customer_image', 'UID'] ]

And while you delete a record from customer_details, check this array to understand which tables' datas also must be deleted.

Bu as I said use this method if you really need to do it with PHP, otherwise you have to do it in MySQL side.

Eray
  • 7,038
  • 16
  • 70
  • 120