Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

update only the changed rows

Posted on 2011-03-09
11
Medium Priority
?
319 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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

971 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