Jump to content

Change order of database fields


unplugged_web
 Share

Recommended Posts

I'm not sure why this isn't working. I have a table of entries, but I'd like to change the running order of the entries. I've created a field called 'priority' and I want the people in the back end to change the order of the entries themselves. So for example, if they move one entry up then the one above will automatically move down. I'm not getting any errors at all, just a blank page.The code I've written is:

 <?php$id = $_GET['id'];$direction = $_GET['direction']; con = mysql_connect("hostname", "user", "password");if  (!$con){die('Could not connect: ' . mysql_error());}mysql_select_db("table", $con); $sql = "SELECT priority FROM Reviews WHERE ID = $id";list($curpos) = mysql_fetch_array(mysql_query($sql) or die(mysql_error())); // Find new positionif ($direction > 0){// To be moved up$sql = "SELECT priority FROM Reviews WHERE priority < '$curpos' ORDER BY priority LIMIT 1";list($newpos) = mysql_fetch_array(mysql_query($sql) or die(mysql_error()));}else{// To be moved down$sql = "SELECT priority FROM Reviews WHERE priority > '$curpos' ORDER BY priority LIMIT 1";list($newpos) = mysql_fetch_array(mysql_query($sql) or die(mysql_error()));} if ($newpos){$sql = "UPDATE Reviews SET priority = $curpos WHERE priority = $newpos";mysql_query($sql) or die(mysql_error()); $sql = "UPDATE Reviews SET priority = $newpos WHERE ID = $id AND priority = $curpos";mysql_query($sql) or die(mysql_error()); if (!mysql_error() && mysql_affected_rows()) $msg = "Review moved"; }mysql_close($con);?>

but this doesn't seem to do anything. I'd be grateful for any help. I have in working perfectly for another table in the same database, but even if I copy the code over and ust change the relevant fields it still doesn't work. The 'priority' field is set to not null and is an integer. Thanks for any help

Edited by thehappyappy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...