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

  • SEARCH
  • Home
  • 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 8310189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:22:20+00:00 2026-06-08T19:22:20+00:00

I’m hosting a website where the content can be added by authorized users. The

  • 0

I’m hosting a website where the content can be added by authorized users. The content is placed within blocks on the webpage. Authorized users are able to create new blocks and to put the blocks in a specific order. The order and content of the blocks are stored within a database.

When new blocks are created the following two MySQL queries are executed:

The order number, of the blocks within the database, are updated with the following query:

UPDATE `Block`
SET `block_order` = `block_order` + 1
WHERE `tab_nr` = $tab_nr
AND `block_order` > $nr;

The new block is added by executing the following query:

INSERT INTO `Block`
  (`id`, `tab_nr`, `block_order`, `html`, `created`)
VALUES
  (NULL, $tab_nr, $nr + 1, '$html', CURRENT_TIMESTAMP);

The problem with those queries is that the first query is executed no matter what happens with the second query. If the second query fails (whatever the reason may be) the order number of the other blocks were still changed by the execution of the first query. This way the block order numbers got messed up.

What I need is a single query that adds the new block to the database and updates the order numbers of the other blocks if and only if there are equally numbered blocks. The problem though is that I couldn’t find a query that does everything I need. For example “ON DUPLICATE KEY UPDATE” will only update the row that conflicts with the new insert, but in my case other rows need to be updated too. Are there any suggestions for a query that get rid of the above mentioned problems?

  • 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-08T19:22:22+00:00Added an answer on June 8, 2026 at 7:22 pm

    You need to use transaction query, as Matt said in his comment.
    Something like this can be helpful.

    START TRANSACTION;
    
    UPDATE Block
    SET block_order = block_order + 1
    WHERE tab_nr = $tab_nr
    AND block_order > $nr;
    
    INSERT INTO Block
      (id, tab_nr, block_order, html, created)
    VALUES
      (NULL, $tab_nr, $nr + 1, '$html', CURRENT_TIMESTAMP);
    
    COMMIT;
    

    So, even if the first query fails, the whole transaction will rollback and save you the mess.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.