rgb192
asked on
update only the changed rows
I am combining php and sql
I am using a php form and I only want to update values where the folderid has been changed by the php form
if I have to update every single row, it may take time, so I only want to update the rows that have been changed by the php form
if I run this
update products set folderid=$folderid where productid=$productid and folderid!=$folderid
then I am still updating every single row, but it may be quicker
what is the quickest way to do this
I am using a php form and I only want to update values where the folderid has been changed by the php form
if I have to update every single row, it may take time, so I only want to update the rows that have been changed by the php form
if I run this
update products set folderid=$folderid where productid=$productid and folderid!=$folderid
then I am still updating every single row, but it may be quicker
what is the quickest way to do this
ASKER
if(isset($_POST['submit']) ) {
foreach($_POST['titleid'] as $id =>$v){
$titleid=$_POST['titleid'] [$id];
$sql="update products set folderid=$titleid where productid=$id and folderid!=$folderid";
echo '<br>'.$sql;
}//end foreachpost
}//end ifisset
echo '<form action="" method="POST"';
<input type="submit" name="submit" value="Submit" />
foreach($products as $p) {
echo'
<td>
<select name="titleid[' . $p['productid'] . ']">';
$this->displayProductFolde rsC($folde rid);
echo' </select>
</td>
';
}
</form>
foreach($_POST['titleid'] as $id =>$v){
$titleid=$_POST['titleid']
$sql="update products set folderid=$titleid where productid=$id and folderid!=$folderid";
echo '<br>'.$sql;
}//end foreachpost
}//end ifisset
echo '<form action="" method="POST"';
<input type="submit" name="submit" value="Submit" />
foreach($products as $p) {
echo'
<td>
<select name="titleid[' . $p['productid'] . ']">';
$this->displayProductFolde
echo' </select>
</td>
';
}
</form>
I presume you do have a index on productid, so the update should run very fast, affecting possibly 1 or 0 rows ...
so, I repeat: what is the problem, aka what exactly are you trying to improve?
so, I repeat: what is the problem, aka what exactly are you trying to improve?
ASKER
I dont want to run a query for each row
I only want to run a query for the rows I change by the php select menu on the form
I only want to run a query for the rows I change by the php select menu on the form
>> I only want to run a query for the rows I change by the php select menu on the form
How do you know the changed records? Do you have a flag/date stamp that indiactes a record got changed?
How do you know the changed records? Do you have a flag/date stamp that indiactes a record got changed?
ASKER
>>Do you have a flag/date stamp that indiactes a record got changed?
this is a form with the product on each row
I can change only the folderid of the product using a select menu
if I change just one row
then all the rows get updated
I am not sure if there is a flag/date stamp
this is a form with the product on each row
I can change only the folderid of the product using a select menu
if I change just one row
then all the rows get updated
I am not sure if there is a flag/date stamp
<< if I change just one row
then all the rows get updated >>
You mean all the other products also getting changed.
then all the rows get updated >>
You mean all the other products also getting changed.
you need then to "store" the "old" value in a hidden field on the form, so you have, on "post/submit", compare the current value with the old value (from the hidden field)... and run the sql only if the values differ ..
ASKER
>>
you need then to "store" the "old" value in a hidden field on the form, so you have, on "post/submit", compare the current value with the old value (from the hidden field)... and run the sql only if the values differ ..
how
>>
You mean all the other products also getting changed.
I have to do update statement for every row even if nothing is being changed
update table set row1=1 where row1=1
you need then to "store" the "old" value in a hidden field on the form, so you have, on "post/submit", compare the current value with the old value (from the hidden field)... and run the sql only if the values differ ..
how
>>
You mean all the other products also getting changed.
I have to do update statement for every row even if nothing is being changed
update table set row1=1 where row1=1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
best explanation
and what, apart from the folderid field, will be changed eventually?
and what is wrong with the update you showed? it shall indeed only update the row if folderid is not the new folderid value already?!