Do I have my syntax wrong?
Definitely YES.
you don't do any error handling.
So, if an error occurs, you have no clue what is it.
at least run your queries this way and see what's wrong with them yourself
mysql_query($query) or trigger_error(mysql_error()." ".$query);
Also, your SELECT(IFNULL(SELECT MAX(posid)+1 FROM userlinks
subselect looks suspicious as it seems you are not using advantage of auto-increment and in danger of race condition.
also, as it already mentioned, you do not escape your strings.
As for your sorting, what I'd do:
- after inserting a row, update it's sort field with id value.
- when we have to swap 2 lines - just swap it's sort values.
here is a code, quite ugly but to give you idea:
$id = intval($_POST['move']);
$place = db("SELECT place FROM $table WHERE id=$id");
if (!$id OR !$place) return(error("id or place is not set"));
if (isset($_POST['up'])) {
$sort = db("SELECT sort FROM $table WHERE id=$id");
$sort2 = db("SELECT max(sort) as msort FROM $table WHEREsort < $sort");
if ($sort2) $id2=db("SELECT id FROM $table WHEREsort = $sort2");
}
if (isset($_POST['down'])) {
$sort = db("SELECT sort FROM $table WHERE id=$id");
$sort2 = db("SELECT min(sort) as msort FROM $table WHEREsort > $sort");
if ($sort2) $id2 = db("SELECT id FROM $table WHERE sort = $sort2");
}
if ($sort2) {
$q1 = "UPDATE $table SET sort=$sort2 WHERE id=$id";
$q2 = "UPDATE $table SET sort=$sort WHERE id=$id2";
db($q1);
db($q2);
}
As for the sanitizing your data, refer to these questions
- PHP secure user variable
- In PHP when submitting strings to the database should I take care of illegal characters using htmlspecialchars() or use a regular expression?
- How to include a PHP variable inside a MySQL insert statement