2

I need a trigger in phpmyadmin for 2 tables.

So there is table stores and tbl_storefinder_stores.
I wanted to insert 4 things (name,adress,longitude,latitude) from the table stores to tbl_storefinder_stores, I get help in the forum by this code

INSERT INTO tbl_storefinder_stores(store_name, store_address, lat, lon)
SELECT name, address, latitude, longitude
FROM stores;

and its work !!

But now I want a trigger that do this automatically like: If I change or add anything in table Stores that it update the table tbl_storefinder_stores automatically, like an updater for the 4 things (name/adress/longitude/latitude), How to do this in the phpmyadmin?

2

2 Answers 2

2

You will need 2 triggers

  • When you add a new record in stores then replicate it to tbl_storefinder_stores

  • When you update some data in stores then update them to tbl_storefinder_stores as well.

Now the 2 tables must have a common key between them and I am assuming name is the key here. The first trigger will look like

delimiter //
create trigger stores_ins after insert on stores
for each row 
begin
 insert into tbl_storefinder_stores
 (store_name, store_address, lat, lon)
 values
 (new.name,new.address,new.latitude,new.longitude);
end;//

delimiter ;

Now when you update it will look like

delimiter //
create trigger stores_upd after update on stores
for each row
begin
 update tbl_storefinder_stores
 set 
 store_address = new.address,
 lat = new.latitude,
 lon = new.longitude
 where store_name = new.name ;
end;//
delimiter ;

Note that in the 2nd trigger I have considered name as a common key between them so if you change the name in the first table then the trigger might not update data since it may not find row in the 2nd table with the modified name.

So if you still want to do so you may do as

delimiter //
create trigger stores_upd after update on stores
for each row
begin
 update tbl_storefinder_stores
 set 
 name = new.name,
 store_address = new.address,
 lat = new.latitude,
 lon = new.longitude
 where store_name = old.name ;
end;//
delimiter ;
14
  • thank you for your answer , is there a way to use the ID as a common key the database table looks like this : i.stack.imgur.com/lpZD6.png , because maybe if i edit a name in the first table that second one will change too , there a another problem that they have different id's how can i synchronize
    – M6Gpower
    Jun 9, 2015 at 10:58
  • Right you should be using ID for the 2nd trigger and store id from first table to the 2nd table as well. So that the update operation is done with respect to ID instead of name and data integrity remains. Jun 9, 2015 at 11:00
  • So how the code look like if i want synchronize the ID's and use the ID's as the Common Key
    – M6Gpower
    Jun 9, 2015 at 11:03
  • I see you already have id in store table and store_id in the 2nd table. So is store_id in the 2nd table is id from first store table ? if not you may first need to truncate the 2nd table and then write insert into select from.. and needs to insert store_id as id from store. Jun 9, 2015 at 11:06
  • I have a error now in my PHP page if i want Edit : Array ( [0] => Invalid query: Unknown column 'name' in 'where clause' ) UPDATE stores SET name='TEST55', cat_id='0', address='Dortmund', telephone='', email='', website='', description='', latitude='51.5135872', longitude='7.465298100000041', approved='1', modified='2015-06-09 11:05:48' WHERE id=733
    – M6Gpower
    Jun 9, 2015 at 11:06
2

you just needs to update on duplicate key right?

Than you can use ON DUPLICATE KEY UPDATE

please Visit for more details here

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.