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 8172999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:58:44+00:00 2026-06-06T21:58:44+00:00

I am having difficulty updating a range of columns using mysql. My overall goal

  • 0

I am having difficulty updating a range of columns using mysql. My overall goal is to give the user the ability to change a list item’s position and to have all of the other list items shift position to accommodate for the change. So say you have a range of numbers, 1-6 and you would like to move the item in position 2 to position 4 and have each item compensate for the change while each occupying only a single position number. I have been working on this for a few hours now and I am getting too tired to think straight. I am still very much a newbie with mysql but I have almost finished making my first cms except for this last annoying tidbit.

The code in question is:

$newposition = $_POST['position'];
$oldposition = $_GET['oldposition'];
$id = $_GET['id'];

while ($work = mysql_fetch_array($workset)) {

if ($newposition>$oldposition) {
    mysql_query('UPDATE work SET position=position-1 WHERE position<='.$newposition.' AND position>'.$oldposition.'');
    mysql_query('UPDATE work SET position='.$newposition.' WHERE id='.$id.'');
}
elseif 
($newposition<$oldposition) {
    mysql_query('UPDATE work SET position=position+1 WHERE position<'.$oldposition.' AND position<='.$newposition.'');
    mysql_query('UPDATE work SET position='.$newposition.' WHERE id='.$id.'');
}
elseif 
($newposition==$oldposition) {
    echo 'same value!  ';
}
}

It creates the requested position change correctly but all of the other numbers in the range get changed to an incorrect value. It is probably a simple mistake..

  • 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-06T21:58:46+00:00Added an answer on June 6, 2026 at 9:58 pm

    So, I came up with a solution for anyone that is interested. It isn’t the cleanest, but it appears to have worked..

    if ($newposition>$oldposition) {
        for ($i=$oldposition+1; $i<=$newposition; $i++) {
            $workset = mysql_query('SELECT * FROM work WHERE position='.$i.' LIMIT 1', $connection);
            while ($work = mysql_fetch_array($workset)) {
                mysql_query('UPDATE work SET position='.$i.'-1 WHERE position='.$i.'');
                mysql_query('UPDATE work SET position='.$newposition.' WHERE id='.$id.'');
            }
        }
    }
    elseif 
    ($newposition<$oldposition) {
        for ($i=$oldposition-1; $i>=$newposition; $i--) {
            $workset = mysql_query('SELECT * FROM work WHERE position='.$i.' LIMIT 1', $connection);
            while ($work = mysql_fetch_array($workset)) {
                mysql_query('UPDATE work SET position='.$i.'+1 WHERE position='.$i.'');
                mysql_query('UPDATE work SET position='.$newposition.' WHERE id='.$id.'');
            }
        }
    }
    elseif 
    ($newposition==$oldposition) {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having difficulty updating a table via PHP. Using MySQL prompt or a
I am having difficulty getting my helper to display a list item. The markup
I'm having difficulty rotating an item a full 360 degrees in webkit using CSS
I am having difficulty using JQuery UI Modal Dialog when submitting a form. The
I'm having difficulty implementing an accordion effect on three different tables using jQuery and
I am having difficulty showing a hidden div when a user selects a radio
I am having difficulty updating records within a database based on the most recent
I'm experimenting with MongoDB using the PHP PECL extension, however I'm having difficulty getting
I am having difficulty finding a list of all of the inbuilt icons in
I'm having difficulty getting the binding to fire off on a user control that

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.