Cam Connor
Cam Connor

Reputation: 1231

Add multiple values to table mysql

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

Upvotes: 2

Views: 160

Answers (2)

Suthan Bala
Suthan Bala

Reputation: 3299

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.

Upvotes: 3

Stephan
Stephan

Reputation: 8090

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');

Upvotes: 2

Related Questions