Php mysql trying to get data from table a and table c putting into a table c?

ganger

New member
basically i have 2 tables 1 called members and one called events

i want to get username from table 1

and get event from table 2

and put them into a new table

what php code do i need

$query =" INSERT INTO ??
i have built a web site were people can join and book certain events so i created 2 tables one for members info and another for events i want to creat a 3rd so can pull the customer username from memebrs table and pull the event and put into the 3rd table which i am going to echo out to the members page to show which events they have booked
i am new to coding bit but doing a coarse so not to sure on how to stop duplicating
 
Are events associated with members? If events has a foreign key to users, then it would be:

INSERT INTO table3(memberID,eventID)
SELECT e.memberID,e.eventID
FROM members m
JOIN events e on e.memberID = m.eventID;

When do things get deleted from events? Or do they?
If not, how do you prevent duplicates on each insert?
You could add a WHERE after the join:
...
JOIN events e on e.memberID = m.eventID
WHERE NOT EXIST (select 1 from table3 t where t.memberID = e.memberID AND t.eventID = e.eventID);

But that's all sort of a guess. What is your actual table structure? What is it you are really trying to do?

ETA: Based on the details: OK, you would do the insert when a member signs up for an event. At that point, you should already have the memberID and eventID. So you would insert a single row with those 2 values:

INSERT INTO table3(memberID,eventID)
VALUES(mID,eID);

Replace mID and eID with the whatever you need here to get the actual numbers in your code.
 
Back
Top