INSERTING MANY ROWS AT THE SAME TIME

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

INSERTING MANY ROWS AT THE SAME TIME

Post by rooky »

HI EVERYBODY,
IS IT POSSIBLE TO HAVE A FORM IN PHP TO ADD MANY ROWS CONTAINING DATA OF PEOPLE THAT ARE REGISTERED FOR A TOUR.
SOMETHING LIKE THIS:

TOUR:______________ DATE:_______________ GUIDE:_____________

CLIENTS NAME ROOM # LANGUAGE
DOWN HERE GOES ALL THE INFORMATION OF EACH CLIENT AND WHEN WE PRESS SUBMIT ALL THIS CLIENTS HAVE TO BE SAVED INTO THE DATABASE.

PLEASE HELP ME
THANKS
SomeoneE1se
Forum Newbie
Posts: 20
Joined: Sat May 31, 2008 2:53 am

Re: INSERTING MANY ROWS AT THE SAME TIME

Post by SomeoneE1se »

try

Code: Select all

 
<input type="text" name="tour[0]" /><input type="text" name="date[0]" /><input type="text" name="guide[0]" />
<input type="text" name="tour[1]" /><input type="text" name="date[1]" /><input type="text" name="guide[1]" />
<input type="text" name="tour[2]" /><input type="text" name="date[2]" /><input type="text" name="guide[2]" />
<input type="text" name="tour[3]" /><input type="text" name="date[3]" /><input type="text" name="guide[3]" />
<input type="text" name="tour[4]" /><input type="text" name="date[4]" /><input type="text" name="guide[4]" />
<input type="text" name="tour[5]" /><input type="text" name="date[5]" /><input type="text" name="guide[5]" />
 
and then use

Code: Select all

 
for($i=0;$i++;$i<=count($_POST[tour]){
    DB_INSERT_FUNCTION($_POST[tour[$i]], $_POST[date[$i]], $_POST[guide[$i]], $OTHER_DATA);
}
 
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: INSERTING MANY ROWS AT THE SAME TIME

Post by WebbieDave »

The INSERT statement allows one to add multiple rows at a time.
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Re: INSERTING MANY ROWS AT THE SAME TIME

Post by rooky »

HI GUYS AND THANKS FOR YOUR QUICK REPLY.
IS IT POSSIBLE TO MAKE THE INSERT IN TWO DIFFERENT TABLES AT THE SAME TIME?
WHAT I REALLY WANT TO DO IS INSERT THE FIRST DATA INTO 1 TABLE WHICH IS: TOUR, DATE AND GUIDE BECAUSE THIS INFORMATION HAS TO BE INSERTED ONLY ONCE AND THE OTHER INFORMATION THAT I NEED TO INSERT MANY RECORDS AT THE SAME TIME IS THIS:
CLIENTS NAME, ROOM NUMBER, LANGUAGE AND VOUCHER

THANKS GUYS!
SomeoneE1se
Forum Newbie
Posts: 20
Joined: Sat May 31, 2008 2:53 am

Re: INSERTING MANY ROWS AT THE SAME TIME

Post by SomeoneE1se »

I've given you the way to do it just edit it as needed.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: INSERTING MANY ROWS AT THE SAME TIME

Post by pickle »

Please stop YELLING AT US
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Re: INSERTING MANY ROWS AT THE SAME TIME

Post by rooky »

Hi SomeoneElse and sorry for asking so many questions, but I'm new with PHP and MySQL so this is why I'm asking for help.
In this sample that you gave me the other day:
for($i=0;$i++;$i<=count($_POST[tour]){
DB_INSERT_FUNCTION($_POST[tour[$i]], $_POST[date[$i]], $_POST[guide[$i]], $OTHER_DATA);
}

On the $i<=count($_POST[tour]) DO I HAVE TO ADD THE OTHER FIELDS HERE ALSO OR ONLY TOUR?

THANKS AND HELP ME PLEASE!
Alan01252
Forum Newbie
Posts: 12
Joined: Sun Aug 03, 2008 3:20 pm

Re: INSERTING MANY ROWS AT THE SAME TIME

Post by Alan01252 »

In the above code he is using tour just to get a count of how many tour input boxes there are...

Type this code and see if it makes more sense for you

Code: Select all

 
echo "<pre>";
 print_r($_POST['tour']);
echo "</pre>";
 
for($i = 0; $i < count($_POST['tour']); $i++)
{
 $sql = "INSERT INTO `TABLE` ('tour','date','guide') VALUES ("$_POST[tour[$i]]","$_POST[date[$i]]","$_POST[guide[$i]]")";
 echo $sql;
 //do query 
}
 
SomeoneE1se
Forum Newbie
Posts: 20
Joined: Sat May 31, 2008 2:53 am

Re: INSERTING MANY ROWS AT THE SAME TIME

Post by SomeoneE1se »

and I also reversed the for loop use alan's

and $i < count() will miss one

it needs to be $i <= count() or $i < count() +1
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: INSERTING MANY ROWS AT THE SAME TIME

Post by Bill H »

and $i < count() will miss one
Actually, it won't. If the $_POST array has six items in it then count() will return 6.
($i =0, i < 6; i++) will iterate for 0-5 which will be six times, and will exercise the loop's functionality on array element 0 thru array element 5 which is correct, since PHP uses zero-based arrays.
rooky
Forum Newbie
Posts: 15
Joined: Thu Jul 31, 2008 10:03 pm

Re: INSERTING MANY ROWS AT THE SAME TIME

Post by rooky »

GUYS, I REALLY THANK YOU VERY MUCH FOR ALL THE HELP YOU GAVE ME. NOW I CAN UNDERSTAND.
SORRY FOR BOTHERING.
SomeoneE1se
Forum Newbie
Posts: 20
Joined: Sat May 31, 2008 2:53 am

Re: INSERTING MANY ROWS AT THE SAME TIME

Post by SomeoneE1se »

Bill H wrote:
and $i < count() will miss one
Actually, it won't. If the $_POST array has six items in it then count() will return 6.
($i =0, i < 6; i++) will iterate for 0-5 which will be six times, and will exercise the loop's functionality on array element 0 thru array element 5 which is correct, since PHP uses zero-based arrays.
F***, how did I screw that one up? I just had an argument with someone explaining your point... god now I feel stupid
Post Reply