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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:39:47+00:00 2026-05-12T13:39:47+00:00

Hey, I have a table with id, name, and weight columns. Weight is an

  • 0

Hey, I have a table with “id”, “name”, and “weight” columns. Weight is an unsigned small int.

I have a page that displays items ordered by “weight ASC”. It’ll use drag-n-drop, and once the order is changed, will pass out a comma-separated string of ids (in the new order).

Let’s say there’s 10 items in that table. Here’s what I have so far:

Sample input:

5,6,2,9,10,4,8,1,3,7

Sample PHP handler (error handlers & security stuff excluded):

<?php
$weight = 0;
$id_array = explode(',', $id_string);

foreach ($id_array as $key => $val)
{
    mysql_query("UPDATE tbl SET weight = '$weight' where id = '$val' LIMIT 1");
    $weight++;
}
?>

When I make a change to column order, will my script need to make 10 separate UPDATE queries, or is there a better way?

  • 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-05-12T13:39:47+00:00Added an answer on May 12, 2026 at 1:39 pm

    You can only specify one where clause in a single query — which means, in your case, that you can only update one row at a time.

    With 10 items, I don’t know if I would go through that kind of troubles (it means re-writing some code — even if that’s not that hard), but, for more, a solution would be to :

    • delete all the rows
    • inserts them all back
    • doing all that in a transaction, of course.

    The nice point is that you can do several inserts in a single query ; don’t know for 10 items, but for 25 or 50, it might be quite nice.

    Here is an example, from the insert page of the MySQL manual (quoting) :

    INSERT statements that use VALUES
    syntax can insert multiple rows. To do
    this, include multiple lists of column
    values, each enclosed within
    parentheses and separated by commas.

    Example:

    INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
    

    Of course, you should probably not insert “too many” items in a single insert query — an insert per 50 items might be OK, though (to find the “right” number of items, you’ll have to benchmark, I suppose ^^ )

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

Sidebar

Related Questions

hey all. I have a table in my DB that has about a thousand
Hey folks, I have a field within a table that I need to do
Hey, I have a table called products that has a relationship with itself. This
Hey, I have a table with 2 fields of type int which are StatusID
Hey I have a name column in my table, suppose i get have name
hey, following problem here: i have table with persons that have one location, if
Hey I have a really annoying IE7 bug that I am trying to work
Hey we have a library class (lib/Mixpanel) that calls delayed job as follows: class
Hey there. I have a c# application that parses txt files and imports the
Hey StackOverflow community, My question is as follows: I have a table, say USER_ADDR

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.