Bobafart
09-29-2007, 04:44 PM
Hi, I am making a "groups" section for my site where users can create their own groups.
When the user creates a new group I INSERT the info into 3 tables: 1) the main group table 2) the members table (lists the members of each group) and 3) the group "tag" table (lists the tags for each group).
The problem has to do with INSERTing the correct $groupid for each of the 3 inserts. The first INSERT creates the groupid as it is an auto_increment as set up when I created the mySQL table. How do I get the same group id and insert it into the other two tables?
So the user fills out the form and a bunch of check take place. If successful the insert code executes:
// insert the name of the group into the main group table
$sql = "INSERT INTO agroups (id, creatorUserid, groupName, groupCat, dateCreatedUnix) VALUES ('', {$_SESSION[userid]}, '$groupName', '$groupCat', unix_timestamp() )";
$result = mysql_query($sql);
// since this user created a new group we have to add him to the member list
$sql = "INSERT INTO agroupsMembers (id, userid, groupid, memberJoinDate) VALUES ('', {$_SESSION[userid]}, $groupid, unix_timestamp())";
$result = mysql_query($sql);
// add the tags the user used when he/she created the group
$sql = "INSERT INTO agroupsTags (id, groupid, groupTag) VALUES ('', $groupid, '$groupTag'";
$result = mysql_query($sql);
the problem that has to do with $groupid. How do I get $groupid when it was created as an auto_increment in the first insert for my main group creation table?
what's the best foolproof way of getting the correct $groupid?
When the user creates a new group I INSERT the info into 3 tables: 1) the main group table 2) the members table (lists the members of each group) and 3) the group "tag" table (lists the tags for each group).
The problem has to do with INSERTing the correct $groupid for each of the 3 inserts. The first INSERT creates the groupid as it is an auto_increment as set up when I created the mySQL table. How do I get the same group id and insert it into the other two tables?
So the user fills out the form and a bunch of check take place. If successful the insert code executes:
// insert the name of the group into the main group table
$sql = "INSERT INTO agroups (id, creatorUserid, groupName, groupCat, dateCreatedUnix) VALUES ('', {$_SESSION[userid]}, '$groupName', '$groupCat', unix_timestamp() )";
$result = mysql_query($sql);
// since this user created a new group we have to add him to the member list
$sql = "INSERT INTO agroupsMembers (id, userid, groupid, memberJoinDate) VALUES ('', {$_SESSION[userid]}, $groupid, unix_timestamp())";
$result = mysql_query($sql);
// add the tags the user used when he/she created the group
$sql = "INSERT INTO agroupsTags (id, groupid, groupTag) VALUES ('', $groupid, '$groupTag'";
$result = mysql_query($sql);
the problem that has to do with $groupid. How do I get $groupid when it was created as an auto_increment in the first insert for my main group creation table?
what's the best foolproof way of getting the correct $groupid?