Page 1 of 1

HELP NEEDED - Multiple same-name form fields

Posted: Tue Jun 08, 2004 5:09 pm
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?

Posted: Tue Jun 08, 2004 9:37 pm
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

Posted: Tue Jun 08, 2004 10:23 pm
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[]

Posted: Tue Jun 08, 2004 10:31 pm
by markl999
Typo:
for ($i=0;i>10;i++){ should of course be for ($i=0;$i<10;$i++){

Posted: Wed Jun 09, 2004 12:35 pm
by evilmonkey
Oops. :oops:

But the logic is there. :D