Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

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
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

how exactly do you identify the rows that are managed by the form?
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?!
Avatar of rgb192

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->displayProductFoldersC($folderid);
        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?
Avatar of rgb192

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
How do you know the changed records? Do you have a flag/date stamp that indiactes a record got changed?
Avatar of rgb192

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
<< if I change just one row
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 ..
Avatar of rgb192

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
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

best explanation