Insert into table from another table mysql

hey guys, so i’m trying to insert data from one table to another.

table 1:
|event_id|event_name|start|end|event_venue|

table 2:
|guest_id|guest_name|event_id|event_name|

i came across Trigger and got something like this:


CREATE TRIGGER after_insert_event AFTER INSERT ON event 
FOR EACH ROW 
    BEGIN
        INSERT INTO guest (event_id, event_name) VALUES (NEW.event_id, NEW.event_name)
    END;

correct if/where its wrong. thanks.

and im kinda at a lost as to where to place this code. i know in database but where?

i decided to just go with what i know for now :


$insertg = mysql_query("INSERT INTO guest(event_id) SELECT event_id FROM event WHERE event_name = '$name' ") or die(mysql_error());

Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.

What is the purpose of those tables? From the looks of it you’re over thinking things.

you’re right. probably. but im no longer doing wtv nonsense i was trying to do anyway.

Continued doubleclick table1 row appends to table2 row