2

I am creating a MySQL database and it is storing stats from a game. Right now I am just setting it up and I need to add 80 players with a column for each one that has there goals, assists, etc...

here is my code:

INSERT INTO `finalproject`.`Bremners` (
   `stud_id`, 
   `stud_name`, 
   `stud_goal`, 
   `stud_assist`,
   `stud_attendance`, 
   `stud_goalie`
) VALUES (
   '1', 
   'test', 
   '0', 
   '0', 
   '0', 
   '0'
);

But that only inserts one player named "test". Does anyone know how to modify this code so that it adds 80 players each one with a different name? (I making the website with php so if it can be done through php that works to)

Thanks

1
  • 1
    Take a look at this. Commented May 10, 2013 at 13:47

2 Answers 2

3

Well, you can start off by creating a PHP array of players, then loop through that array and use mysql to add each of those players into the database.

$players = array("John", "Jack", "Josh"..."80th Player");

foreach($players as $player):
$player = mysql_real_escape_string($player);
mysql_query("INSERT INTO `finalproject`.`Bremners` (`stud_id`, `stud_name`, `stud_goal`, `stud_assist`,
`stud_attendance`, `stud_goalie`) VALUES ('1', $player, '0', '0', '0', '0')")
endforeach;

I wouldn't use the mysql_.. to insert into database but just to give you the idea I used it.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmmm, and execute 80 queries instead of 1? Seems like a waste to me.
The real waste if you ever want to re-use the game is typing in 80 players as SQL, instead of having an interface where you input each player as you like, or the player registers.
@Adder I think he's inserting these rows for test purposes. He might not have created any interface yet. However, if there are only 80 rows using the PHP loop and send 80 queries is not as bad idea, as this solution is simple and has to be done (I suppose) only once.
2

Try this

INSERT INTO `finalproject`.`Bremners` (
     `stud_id`, 
     `stud_name`, 
     `stud_goal`, 
     `stud_assist`,
     `stud_attendance`, 
     `stud_goalie`
)VALUES 
('1', 'test', '0', '0', '0', '0'),
('2', 'test1', '0', '0', '0', '0'),
('3', 'test2', '0', '0', '0', '0'),
....
('80', 'test79', '0', '0', '0', '0');

4 Comments

If all stud_* columns have default values, probably , '0', '0', '0', '0' would not be necessary.
Some thoughts that might make your life better use an auto increment column for stud_id so that you can let the db manage that id for you by using insert.. values(null, ..) the db will increment the id.
@Voitcus i agree it would be much simpler :)
@mconlin yes that's also a valid point with your advice and the one from Voitcus you can reach something like INSERT INTO table (name) VALUES ('test'),('test1')...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.