Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update multiple records from one form

Status
Not open for further replies.

neilmcdonald

Technical User
Aug 16, 2002
53
Hi,

I've seen several posts regarding updating several MySQL records from one form, but I can't get my head around how to make it work for me...

I have set up a form which contains a table. Each row contains one job, which included a jobid and a drop-down of job status.

What I need is to be able to update the status for several jobs in one form.

I've managed to create an array with the correct information in by setting the name of the drop-down input to be an array.

What I can't figure out is how to set the key to be the jobid, so that when the form is posted, I can then loop through the values.

I'd be grateful for any advice,

Thanks,

Neil
 
whether you can change multiple records in the same query very much depends on exactly what you want to do.

you can only run one query at a time. so something like this

Code:
mysql_query("Update tablename set datemodified=".time()." if primarykey IN (".explode (','$_POST['id']).")";
would work (assuming that the record id's were available in an array called $_POST['id']

however you cannot update two separate records easily with separate data
you must run multiple queries. so assuming a bank account reconciliation example

Code:
foreach ($_POST['transactionID'] as $key=>$txID){
  mysql_query ("Update accountstable set amount=$_POST['amount'][$key] where transactionID=$val");
}

this assumes that the form is made up of lots of identical rows with the html control names identically named but with aa suffix of "[txid]" where the txid is the transaction id for that row. this is an ok approach but somepeople find it easier to reverse the logic and call each control something like this
Code:
<input type="text" name="transaction[][amount']"/>
in which case the loop in the PHP code would be

Code:
foreach ($_POST['transaction'] as $transaction){
  $amount = $transaction['amount'];
  $txID = $transaction['txid'];
  //etc
}
 
Hi jpadie,

Thanks for the quick response!

The code I had in mind for the processing is very much like your last example. Could you possibly explain further how the txid in your example would be entered into the array from the form?

Sorry if this a dumb question - I'm pretty new to PHP.
 
you would include the txID in a hidden control in the same way as any other field
Code:
<input type="hidden" name="transaction[][txID]" value="12345" />

there are many ways to skin this cat: just adopt the one that seem most logical to you as a structure for your resultant arrays.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top