Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-04-2012, 04:31 PM   PM User | #1
tim_poole247
New Coder

 
Join Date: Feb 2009
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
tim_poole247 is an unknown quantity at this point
issue with adding multiple entries to database from one query

Hi there, I am currently working on a browser based game written in PHP. The issue im coming to at the moment is with adding multiple entries to a db table with only one query.

I have a db table called 'user_accounts' where all the users details are stored. In this table is a field called 'powers'. 3 values, 1 = server admin, 2 = game admin, 3 = normal user. Another table called '$db_name'_users where '$db_name' is a unique value to the game. On the server there will be 4 games running in total. Basically I want to add all the users with the power lvl of 1 (so all the server admins) from user_accounts to the dbname_users table. Obviously I can do this manually, but im making an automated installation script, so would like to add them all in one go. I can't use a generic user as the server owner creates the server admins in a previous step in the install script, so would be unique to each application of the game.

here is the first code I tried:
Code:
db(__FILE__,__LINE__,"SELECT login_id, login_name, email_address FROM user_accounts WHERE powers = 1");
		$server_ads = dbr();
		while($server_ads) {
		dbn(__FILE__,__LINE__,"INSERT INTO $_POST[_dbname]_users (login_id, login_name, cash, tech, genesis, terra_imploder, alpha, gamma, delta, sn_effect, grav_mine, hornet_mine, leech, email_address) VALUES ('$server_ads[login_id]', '$server_ads[login_name]', '99999999999', '99999999', '999999', '999999', '999999', '999999', '999999', '999999', '999999', '999999', '999999', '$server_ads[email_address]')");
		}
this correctly selected a user who matched the criteria and added it to the correct db table.. however it tried to repeatedly add the same user and as the login_id field is unique I got a duplicate error in the sql.

the next tweak I tried was to put while($server_ads = dbr()) instead of while($server_ads) again this selected the correct user and put it into the right table, however it only added one user, when there are 3 users who match the criteria. Anyone got any ideas?
tim_poole247 is offline   Reply With Quote
Old 11-04-2012, 04:35 PM   PM User | #2
tim_poole247
New Coder

 
Join Date: Feb 2009
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
tim_poole247 is an unknown quantity at this point
... after posting I realised what an idiot I was being... while($server_ads = dbr()) is working fine... I just forgot about the $server_ads = dbr();
on the line above.... omg.. been a long weekend
tim_poole247 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:54 AM.


Advertisement
Log in to turn off these ads.