Solved

update only the changed rows

Posted on 2011-03-09
11
258 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 142

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 142

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
 

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 40

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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 40

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 142

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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now