sngumo Posted January 31, 2009 Share Posted January 31, 2009 Hi I have been frustrated trying to create a function or MySql query which will switch the values (same table column) of 2 rows in the same table. Here is the code *********************************************** connect(); $location = $_GET['location']; $old_position = $_GET['oldpos']; $new_position = $_GET['newpos']; $move[0] = "UPDATE menu SET menu_order = '$old_position' WHERE menu_order = '$new_position' AND accesslevel = '$location'"; $move[1] = "UPDATE menu SET menu_order = '$new_position' WHERE menu_order = '$old_position' AND accesslevel = '$location'"; for($i=0;$i<=1;$i++) { mysql_query($move[$i]); } ********************************************** Once I run the code it only performs the first query. Please I need your assistance Thanx Quote Link to comment https://forums.phpfreaks.com/topic/143241-update-query-to-switch-the-values-of-2-table-rows-php-mysql/ Share on other sites More sharing options...
sasa Posted January 31, 2009 Share Posted January 31, 2009 UPDATE menu SET `menu_order` = IF( menu_order =$old_position, $new_position, $old_positon ) WHERE menu_order IN ( $old_position, $new_position ) Quote Link to comment https://forums.phpfreaks.com/topic/143241-update-query-to-switch-the-values-of-2-table-rows-php-mysql/#findComment-751241 Share on other sites More sharing options...
sngumo Posted February 1, 2009 Author Share Posted February 1, 2009 Thanks Sasa a lot for giving me that code It worked perfectly but I dont quite understand how it works!!!! particularly the IF clause would you mind explaining it to me Thanks Quote Link to comment https://forums.phpfreaks.com/topic/143241-update-query-to-switch-the-values-of-2-table-rows-php-mysql/#findComment-751817 Share on other sites More sharing options...
sasa Posted February 1, 2009 Share Posted February 1, 2009 http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if Quote Link to comment https://forums.phpfreaks.com/topic/143241-update-query-to-switch-the-values-of-2-table-rows-php-mysql/#findComment-751819 Share on other sites More sharing options...
sngumo Posted February 2, 2009 Author Share Posted February 2, 2009 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/143241-update-query-to-switch-the-values-of-2-table-rows-php-mysql/#findComment-752731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.