INSERTING MANY ROWS AT THE SAME TIME
Moderator: General Moderators
INSERTING MANY ROWS AT THE SAME TIME
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
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
try
and then use
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]" />
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
The INSERT statement allows one to add multiple rows at a time.
Re: INSERTING MANY ROWS AT THE SAME TIME
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!
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
I've given you the way to do it just edit it as needed.
Re: INSERTING MANY ROWS AT THE SAME TIME
Please stop YELLING AT US
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: INSERTING MANY ROWS AT THE SAME TIME
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!
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!
Re: INSERTING MANY ROWS AT THE SAME TIME
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
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
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
and $i < count() will miss one
it needs to be $i <= count() or $i < count() +1
- 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
Actually, it won't. If the $_POST array has six items in it then count() will return 6.and $i < count() will miss one
($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.
Re: INSERTING MANY ROWS AT THE SAME TIME
GUYS, I REALLY THANK YOU VERY MUCH FOR ALL THE HELP YOU GAVE ME. NOW I CAN UNDERSTAND.
SORRY FOR BOTHERING.
SORRY FOR BOTHERING.
-
SomeoneE1se
- Forum Newbie
- Posts: 20
- Joined: Sat May 31, 2008 2:53 am
Re: INSERTING MANY ROWS AT THE SAME TIME
F***, how did I screw that one up? I just had an argument with someone explaining your point... god now I feel stupidBill H wrote:Actually, it won't. If the $_POST array has six items in it then count() will return 6.and $i < count() will miss one
($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.