Page 1 of 1

Looping in PHP to insert rows ?

Posted: Tue Jul 01, 2003 9:21 pm
by mjhaston
I'd like to submit up to 25 rows of scores at a time. If I name my fields
score1, team1 ...
score2, team2 ...

How do I loop in PHP? I tried the code below that is commented out, but no go. I can get a record into my MySQL database, no problem. I could get one very long record or row into my database with all the scores, but that would be hidious! Thanks if you can help, or even if you can't!

Code: Select all

$result = mysql_query("INSERT INTO weeklyscores (assoc_name, league_name, submitted_by, email, week, game_date) 
VALUES ('$assoc_name', '$league_name', '$submitted_by', '$email', '$week', '$game_date')"); 

//for($count;$count<5)$count++) 
//&#123;
//insert into weeklyscores $_POST&#1111;"score_a_".$count."" ] if score_a_$count !="" ;
//&#125;

Posted: Tue Jul 01, 2003 10:00 pm
by Stoker
instead of looping and doing multiple queries it is much more efficent to do it all in one query..

Code: Select all

INSERT INTO tablename
  (column1,column2,column3)
 VALUES
  ('1 case','Sam Adams','Boston Lager'),
  ('1 can','Guiness','Irish Stout'),
  ('1 keg','Sierra Nevada','Pale Ale')

Posted: Wed Jul 02, 2003 3:13 pm
by mjhaston
Stoker I knew you were my density when I saw your avatar! My login to a hockey site I frequent is "I_see_dumb_people"!

Your solutions works like a charm. Can't tell me how much headache you just saved me. I really appreciate the help.

I love you man! (ha, just kidding)

PS - what if I have 20 rows on my form, but they enter only 10 rows of information? Can I check for null or something?

:: inserted later ::

Here's what I got working. Very crude, but working. If there is a better suggestion I'm open. This is so that I don't insert 25 rows each time if there is no data to be found.

Code: Select all

if ($team_a_1 != "Select one ...")&#123;
	mysql_query("INSERT INTO weeklyscores (assoc_name, submitted_by, email, week, game_date, home_team, home_team_score, away_assoc, away_team, away_team_score)
	VALUES ('$assoc_name', '$submitted_by', '$email', '$week', '$game_date', '$team_a_1', '$score_a_1', '$assoc_b_1','$team_b_1', '$score_b_1')");
&#125;