Page 1 of 1
INSERTING MANY ROWS AT THE SAME TIME
Posted: Thu Jul 31, 2008 10:11 pm
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
Re: INSERTING MANY ROWS AT THE SAME TIME
Posted: Thu Jul 31, 2008 10:48 pm
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);
}
Re: INSERTING MANY ROWS AT THE SAME TIME
Posted: Fri Aug 01, 2008 8:32 am
by WebbieDave
The
INSERT statement allows one to add multiple rows at a time.
Re: INSERTING MANY ROWS AT THE SAME TIME
Posted: Fri Aug 01, 2008 12:31 pm
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!
Re: INSERTING MANY ROWS AT THE SAME TIME
Posted: Fri Aug 01, 2008 7:50 pm
by SomeoneE1se
I've given you the way to do it just edit it as needed.
Re: INSERTING MANY ROWS AT THE SAME TIME
Posted: Sun Aug 03, 2008 1:24 am
by pickle
Please stop YELLING AT US
Re: INSERTING MANY ROWS AT THE SAME TIME
Posted: Sun Aug 03, 2008 2:17 pm
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!
Re: INSERTING MANY ROWS AT THE SAME TIME
Posted: Sun Aug 03, 2008 3:37 pm
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
}
Re: INSERTING MANY ROWS AT THE SAME TIME
Posted: Sun Aug 03, 2008 6:48 pm
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
Re: INSERTING MANY ROWS AT THE SAME TIME
Posted: Sun Aug 03, 2008 6:58 pm
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.
Re: INSERTING MANY ROWS AT THE SAME TIME
Posted: Sun Aug 03, 2008 8:26 pm
by rooky
GUYS, I REALLY THANK YOU VERY MUCH FOR ALL THE HELP YOU GAVE ME. NOW I CAN UNDERSTAND.
SORRY FOR BOTHERING.
Re: INSERTING MANY ROWS AT THE SAME TIME
Posted: Sun Aug 03, 2008 9:11 pm
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