Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
|
|
What Is The Easiest Way To Do This?
This is a discussion on What Is The Easiest Way To Do This?, within the MySQL section. This forum and the thread "What Is The Easiest Way To Do This?" are both part of the Programming Your Website category.
![]() ![]() |
Aug 12 2008, 09:38 AM
Post
#1
|
|
|
Master of the Universe ![]() Posts: 1,298 Joined: 15-February 08 From: London, England |
Honestly, there must be an easy way to do this.
I have 2 tables. Users Groups When a user registered they are added to the Users table. Their user id is automatically assigned. I also need to add the user to the 'groups' table. The 'groups' table contains user_id and group_id. Is there some SQL magic that allows me to insert a user into the users table, grab the user_id and insert the user into groups table? -------------------- |
|
|
Aug 12 2008, 09:43 AM
Post
#2
|
|
![]() Co-Founder ![]() Posts: 3,107 Joined: 13-February 08 From: my little igloo up north |
without using 3 sql statements? I don't know ... don't think so
-------------------- a web design portfolio | web non-sense - REDESIGNED!
I'm also on: del.icio.us | flickr | virb | facebook | twitter The Web Squeeze is also on: twitter | virb | facebook | stumbleupon |
|
|
Aug 12 2008, 09:58 AM
Post
#3
|
|
|
Master of the Universe ![]() Posts: 1,298 Joined: 15-February 08 From: London, England |
without using 3 sql statements? I don't know ... don't think so I have always used 3 SQL statements. Just seems like a long way to do a simple task. -------------------- |
|
|
Aug 12 2008, 10:00 AM
Post
#4
|
|
![]() Co-Founder ![]() Posts: 3,107 Joined: 13-February 08 From: my little igloo up north |
-------------------- a web design portfolio | web non-sense - REDESIGNED!
I'm also on: del.icio.us | flickr | virb | facebook | twitter The Web Squeeze is also on: twitter | virb | facebook | stumbleupon |
|
|
Aug 12 2008, 10:03 AM
Post
#5
|
|
![]() Rapid Squeezer ![]() Posts: 199 Joined: 14-February 08 From: Willich, Germany |
nope, you can only INSERT into one table at a time.... (as far as I know....)
I'd love to be proved wrong!!! -------------------- www.c010depunkk.com ~ the hangout of a web developer
|
|
|
Aug 12 2008, 10:19 AM
Post
#6
|
|
|
Fresh Squeezed ![]() ![]() Posts: 30 Joined: 6-March 08 From: Birmingham |
Actually, you can do it in two statements!
People normally use the mysql_insert_id function to obtain the automatically generated value from the first INSERT. But you can shortcut because mysql gives you the LAST_INSERT_ID() function that you can use directly in your SQL statements. For example CODE INSERT INTO groups (user_id, group_id) VALUES (LAST_INSERT_ID(), 5); The full documentation can be found at http://dev.mysql.com/doc/refman/6.0/en/inf..._last-insert-id -------------------- Viventic Web Design and Application Development
|
|
|
Aug 13 2008, 03:22 AM
Post
#7
|
|
![]() Rapid Squeezer ![]() Posts: 199 Joined: 14-February 08 From: Willich, Germany |
that's a nice little trick!! thanks for sharing
-------------------- www.c010depunkk.com ~ the hangout of a web developer
|
|
|
Oct 12 2008, 02:01 PM
Post
#8
|
|
|
Squeezing ![]() ![]() ![]() Posts: 81 Joined: 18-February 08 |
That sounds useful since it says the ID generated is maintained on a per connection basis, so you won't have problems on a busy server like you could with mysql_insert_id.
Dave |
|
|
Oct 12 2008, 06:48 PM
Post
#9
|
|
![]() Squeeze Machine ![]() Posts: 508 Joined: 7-October 08 From: Australia |
Slightly off-topic, sort of, but why does the Groups table have a "user_id"? Shouldn't the Users table have a "group_id", and thus belongs to that group?
Maybe I'd need to see the rest of the table structure to understand... but how are you making more than one user a member of the same group? -------------------- The more you visit, the more I'll post: http://japheththomson.com/
|
|
|
Oct 13 2008, 12:27 AM
Post
#10
|
|
|
Rapid Squeezer ![]() Posts: 308 Joined: 15-February 08 From: Finland |
Going really offtopic here but I would use three tables. Users/groups/usergroups, the third table would be a joining table which would allow users to be part of multiple groups so it would have these two columns (userid, groupid).
LAST_INSERT_ID is a good choice anytime since otherwise you'd have to get the id based on the values and that doesn't sound so foolproof. You cannot insert into two tables at once. -------------------- |
|
|
Oct 13 2008, 12:40 AM
Post
#11
|
|
![]() Squeeze Machine ![]() Posts: 508 Joined: 7-October 08 From: Australia |
@Antti: I agree!
This would obviously be much easier if you're using some kind of framework, and an MVC approach, then you likely won't even have to worry about MySQL... but it might be a steep learning curve. -------------------- The more you visit, the more I'll post: http://japheththomson.com/
|
|
|
Oct 13 2008, 01:27 AM
Post
#12
|
|
![]() Rapid Squeezer ![]() Posts: 199 Joined: 14-February 08 From: Willich, Germany |
When inserting into numerous tables, you may want to check out Transactions (BEGIN, ROLLBACK, COMMIT) to ensure that all the data was inserted before commiting it.
-------------------- www.c010depunkk.com ~ the hangout of a web developer
|
|
|
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:






Aug 12 2008, 09:38 AM









