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?
HELP NEEDED - Multiple same-name form fields
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
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- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
would have to be
other then that the field name would be just milage[]
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- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada