?
Solved

update only the changed rows

Posted on 2011-03-09
11
Medium Priority
?
314 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35087514
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?!
0
 

Author Comment

by:rgb192
ID: 35087573
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>
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35087636
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?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:rgb192
ID: 35087885
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
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35088552
>> 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?
0
 

Author Comment

by:rgb192
ID: 35089271
>>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
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35089340
<< if I change just one row
then all the rows get updated >>

You mean all the other products also getting changed.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35092488
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 ..
0
 

Author Comment

by:rgb192
ID: 35096692
>>
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
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 35100303
your form has currently 2 fields, 1 for the folderid, and 1 for the productid.
on postback, you will get the 2 values from the <input> fields
if you create another <input id=origfolderid type=hidden value=$folderid /> field in the form, on postback of the form, you will find the $_POST["origfolderid"] value, which is the original value of the field folderid, as queried from the database when showing the form ...
0
 

Author Closing Comment

by:rgb192
ID: 35217490
best explanation
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question