Reputation: 267
walls
(4, 'wall4', '125', '574', '../.../thumbnail_wall4.jpg'
)
types
('1', 'Desktop'
)
('2', 'Phone'
)
('3', 'Tablet'
)
wall_types
(4, 1
)
Above are the tables I have, what I want to do is automatically get the wall_id = 4
from walls
tables and store it in wall_types
table, same with the type_id
from types
table. How can this be done? what is the code (PHP code) or SQL statements to achieve this? thanks!
Note:
wall_id
in wall_types
references wall_id
in walls
table.
type_id
in types
references type_id
in types
table.
Upvotes: 0
Views: 1349
Reputation: 1791
The query below inserts a new relation in your wall_types table, where '1' comes from user input (user selected 'Desktop'):
INSERT INTO wall_types (wall_id, type_id) VALUES (5,1);
If you want to do this right after you inserted a new 'wall' row where the id is (off course) unknown yet, you can use LAST_INSERT_ID(), this will contain the last inserted id, therefore you need to execute this query right after the 'wall' insert query:
INSERT INTO wall_types (wall_id, type_id) VALUES (LAST_INSERT_ID(),1);
Upvotes: 1
Reputation: 1
If you want to do this "automatically" you can use Triggers to update the relashionship. The other way is remove the relationship in wall_types and re-create with the updated values.
Upvotes: 0