How to update "sorting_order" column of other rows when changing one?
Add new category :

    $t_cat = 'jeans';//new category to insert
    $so_to_set = 2;// so for new category


First update all so greater than or equal to $so_to_set

    $query = "update categaries set so = so+1 where so &gt;= $so_to_set";


Now insert new category with the $so_to_set

    $query = "insert into categaries(name,so) values($t_cat,$so_to_set)";



Change so for categories :

suppose we have following categaries with so

    +-------------------------+
    | id  |   name    | so    |
    +-----+-----------+-------+
    | 1   |   shoes   |  1    |
    | 6   |   jeans   |  2    |
    | 2   |   shirts  |  3    |
    | 3   |   pants   |  4    |
    | 4   |   blazzers|  5    |
    | 5   |   bags    |  6    |
    +-------------------------+

    $a = 2;// so of category whose position we want to change
    $b = 5;// new so for category

    // get id of category whose position we want to change
    $temp_id = "select id from categaries where so = $a"; 
    // that would be 6


Now update all so between $a and $b

    if($a &lt; $b){
        // if $a is less than $b then
        $set = "so = so - 1";
        $where = "so &gt; $a and so &lt;= $b";
    }else{
        $set = "so = so + 1";
        $where = "so &lt; $a and so &gt;= $b";
    }

    // update so's
    $query = "update categaries set $set where $where";


now update so for $temp_id

    $query = "update categaries set so = $b where id = $temp_id";


Final output of the process would be

    +-------------------------+
    | id  |   name    | so    |
    +-----+-----------+-------+
    | 1   |   shoes   |  1    |
    | 6   |   jeans   |  5    |
    | 2   |   shirts  |  2    |
    | 3   |   pants   |  3    |
    | 4   |   blazzers|  4    |
    | 5   |   bags    |  6    |
    +-------------------------+