Re: update more "same" fields at the same time
| From: | Chris Hayes | Date: | Wed, 15 Jan 2003 13:50:31 +0000 |
| Subject: | Re: update more "same" fields at the same time | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-131702@lists.php.net to get a copy of this message | ||
At 14:29 15-1-03, you wrote:
not with the same data You can put your data and the primary keys in arrays and exec a loop but the data[0] must be the data for the id[0], data[1] must be the data for id[1] etc. The loop should be something like this: for (int $i=0; $i<=total_records_select_returned -1; $i++)That would work good and is shorter than what i did (and accidently mailed to myself in stead of to this list): You split the record ID and value in two form elements while I lump the fieldname with the recordID into the form element name.update table set field1='$data[$i]' where id=$id[$i];I 'm not sure for the syntax but the idea is correct.
The way i understand the question is, you have several records (or rows) in one table that you want to edit on the same page.I have table with six records in it. I can use "while" to display them all, but in form. Is there any way I can edit all six records, and update them all with one submit.
One of the many ways to do it:
When writing the form, number the fieldnames with the ID's of the items. Separate name and ID with a clear character like '__'
<input type="text" name="field__531">
<input type="text" name="field__532">
<input type="text" name="field__533">
So to make this do something like
1. make a query to select the items, select the values you want to show as well as the record's ID field, from now I will call that $recordID.
Echo a <form method="POST" action="whatever.php"> tag
2. Loop through the query result with your favourite loop structure and in the loop echo each record somehow like this:
echo ' <input type="text" name="'.fieldname__$recordID.'" value='.$recordfield.'>';
In the receiving page, split the name again. Then build a query for every record.With fieldname 'fieldname':
while (list ($key, $val) = each ($_POST)) //walk through all form results, key is form_element_name
//and value is value.
{if (! strpos($key,'fieldname')===false) //only with variables starting with the fieldname
{ $keybits=explode('__',$key);$recordID=$keybits[1]; //split the recordID out of the name of the variable
$query="UPDATE mytable SET fieldname='.$value.' WHERE recordID=$recordID";
mysql_query($query) or die('Problem updating data );
}
}