Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:53:49+00:00 2026-06-01T16:53:49+00:00

I need to do two updates to rows but I need to make sure

  • 0

I need to do two updates to rows but I need to make sure they are done together and that no other query from another user could interfere with them. I know about SELECT...FOR UPDATE but I imagine after the first update it will of course be unlocked which means someone could interfere with the second update. If someone else updates that row first, the update will work but will mess up the data. Is there anyway to ensure that the two updates happen how they are supposed to? I have been told about transactions but as far as I know they are only good for making sure the two updates actually happen and not whether they happen “together,” unless I am mistaken and the rows will be locked until the transaction is committed?

Here are the queries:

SELECT z FROM table WHERE id='$id'

UPDATE table SET x=x+2 WHERE x>z

UPDATE table SET y=y+2 WHERE y>z

I made a mistake and didn’t give full information. That was my fault. I have updated the queries. The issue I have is that z can be updated as well. If z is updated after the SELECT but before the other two updates, the data can get messed up. Does doing the transaction BEGIN/COMMIT work for that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:53:50+00:00Added an answer on June 1, 2026 at 4:53 pm

    Learn about TRANSACTION
    http://dev.mysql.com/doc/refman/5.0/en/commit.html

    [... connect ...]
    
    mysql_query("BEGIN");
    
    $query1 = mysql_query('UPDATE table SET x=x+2 WHERE x>y');
    $query2 = mysql_query('UPDATE table SET y=y+2 WHERE y>y');
    
    if($query1 && $query2) {
        mysql_query("COMMIT");  
        echo 'Save Done. All UPDATES done.';  
    } else {
        mysql_query("ROLLBACK");  
        echo 'Error Save. All UPDATES reverted, and not done.';  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that has a group of 3 text_fields. I need two
I need to compare two row with each other and then write the fields
I have a Total value that I need to distribute among several rows in
we need to compare the content of 2 identical gridviews and extract rows that
I need to create two controls that contain the same amound of items (a
I have two tables called clients, they are exactly the same but within two
I need to update two tables inside a single transaction. The individual queries look
I need to update a group of cells by inserting the same two characters
I need two divs to look a bit like this: | | ---| LOGO
I need two methods one to encrypt and one to decrypt an xml file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.