I have a simple database of the following form :
Users{
UserID (PK)
Username
Email
FirstName
SecondName
}
Groups{
GroupID (PK)
GroupName
}
Membership{
UserID (FK)
GroupID (FK)
}
Membership is the “go-between” for the other two tables. When a user registers, their Username is inserted into the Users and Groups table (creating a group by themselves). UserID and GroupID should reference the corresponding columns in Users and Groups.
Is the best way of doing this simply with three INSERT INTO statements when a user registers, like so :
if (db.Execute("INSERT INTO Users(Username, Email, FirstName, SecondName) VALUES(@0,@1,@2,@3)", username, email, firstName, secondName) < 1
|| db.Execute("INSERT INTO Groups(GroupName) VALUES(@0)", username) < 1
|| db.Execute("INSERT INTO Membership(UserID, GroupID) VALUES(@0, @1)",?????????????) < 1)
{
<p class="error">Wasn't able to insert User record</p>//insert failed
}else {
Response.Redirect("Success.cshtml"); //success
}
What should I put in the third INSERT INTO statement so that it will insert the userID and corresponding groupID correctly?
This is database logic… put it in the database.
Now you should be able to say something like this (pardon me if I don’t have your client syntax correct, but I’m trying to come close):