HELP NEEDED - Multiple same-name form fields

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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

HELP NEEDED - Multiple same-name form fields

Post by RobertGonzalez »

I am building an update tool for a database interface that I want to have the capability to update multiple records but in the same field. The records are vehicle data. I want to be able to pull up about 20 vehicle records and enter a mileage for any said number of vehicles, hit submit and have all the vehicle records updated with the now current mileage for each record. Can someone help me with this?

I think I should be using an array for the field name and the record_id as the array index. Is this correct?
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Basically, you can do this inside a 'for' loop. What you need is an array for the index and an array for milage. The field and the milage arrays must have a corresponding indes, meaning that if $index[1] is your never-driven Honda Accord, then $milage[1] would be 0. If $index[10] is an old beat up camry, then $milage[10] would be 400,000. See what I mean? The $index array stores the value of the id field of the database. The number of the index should be in order from 0 to however many fields you're updating. Here's how I'd update:

Code: Select all

//connections are already done
for $i=0;i>10;i++{ //double check that structure, been a while since I last wrote a for loop in PHP. Also, change the 10 to however many records you're updating
$sql="UPDATE cars SET milage=".$milage[$i]." WHERE id=".$index[$i];
$result=mysql_query($sql, $dbconn);
}
//etc
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

would have to be

Code: Select all

//connections are already done
for ($i=0;i>10;i++){ //(inserted ()...double check that structure, been a while since I last wrote a for loop in PHP. Also, change the 10 to however many records you're updating
$sql="UPDATE cars SET milage=".$milage[$i]." WHERE id=".$index[$i];
$result=mysql_query($sql, $dbconn);
}
//etc
other then that the field name would be just milage[]
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Typo:
for ($i=0;i>10;i++){ should of course be for ($i=0;$i<10;$i++){
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Oops. :oops:

But the logic is there. :D
Post Reply