Kemik Posted July 25, 2007 Share Posted July 25, 2007 Hello, I'm developing a league script. I'm creating the createteam.php file and want to create a record (the team) in the teams table and then create a record linking the team and the user who is creating the team (i.e. showing the user is in the team). I'm using the following queries in a function... The function <?php $database->addNewClan($subname, $subtag, $subwebsite, $subhistory, $username) ?> The queries <?php function addNewClan($name, $tag, $website, $history, $username){ $time = time(); $q = "INSERT INTO ".TBL_CLANS." VALUES ('$name', '$tag', '$website', '$history', $time)"; return mysql_query($q, $this->connection); } ?> Is there anyway to create a second query with the newly created teamid that is attached to the new team or would I have to select the record first to discover the teamid. The teamid field is an auto_increment in the database. The new query needs to be along the lines of: $q = "INSERT INTO ".TBL_CLAN_MEMBERS." VALUES ('teamid', '$username', $time)"; You can change the variable name if you wish. I would prefer keeping it all within the one function as the function is used in an IF statement... <?php if($database->addNewClan($subname, $subtag, $subwebsite, $subhistory)){ return 0; //New user added succesfully }else{ return 2; //Registration attempt failed } ?> Thanks all. Link to comment https://forums.phpfreaks.com/topic/61735-inserting-in-to-two-tables/ Share on other sites More sharing options...
Barand Posted July 25, 2007 Share Posted July 25, 2007 After inserting the first record, call $newid = mysql_insert_id(); to get the id of the newly added record. Use that when inserting the related record/s Link to comment https://forums.phpfreaks.com/topic/61735-inserting-in-to-two-tables/#findComment-307438 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.