Two inserts in PHP/MySQL using LAST_INSERT_ID() and rows from another table

前端 未结 3 1384
耶瑟儿~
耶瑟儿~ 2021-01-16 06:51

I am working on an events calendar using PHP and MySQL (V5.1) where the admin would add an event, and then an attendance list for that event would be created as well. I have

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-16 07:28

    You could use mysql_insert_id()

    $query1 = mysql_query("INSERT INTO events (name , location , date) VALUES ('".mysql_real_escape_string($name)."' , '".mysql_real_escape_string($location)."' , '".mysql_real_escape_string($date)."')");
    
    $insert_id = mysql_insert_id() ;
    
    $query2 = mysql_query("INSERT INTO attendance (event_ID , member_ID) SELECT {$insert_id}, members.member_ID FROM members") ;
    

提交回复
热议问题