Page 1 of 1

Mysql DELETE problem

Posted: Fri Sep 29, 2006 2:07 pm
by arukomp
Hello,

I have a code which should delete user and all it's data. Here's the code:

Code: Select all

if ($action == 'delete') {
	$id = $_GET['id'];
	$sql = "DELETE * FROM `users` WHERE id = $id";
	$result = mysql_query($sql);
	$sql = "SELECT id FROM users ORDER BY id DESC LIMIT 1"; // this is to get the highest id
	$result = mysql_query($sql);
	$re = mysql_fetch_array($result);
	$max = $re['id']; // highest id
	$id += 1;
	while($id <= $max){  // this updates all user's ids bellow the deleted user
		$sql = "UPDATE users SET id = id - 1 WHERE id = $id";
		$result = mysql_query($sql);
		$id += 1;
	} // while
}
if() sentence executes successfully, but user is not deleted and ids are not updated. while() sentence doesn't even execute. I have put echo "hello"; in while() to test if it has been executed and echo didn't execute.

Maybe someone sees a problem here?

Any help would be appreciated.

Thanks

Posted: Fri Sep 29, 2006 2:27 pm
by Burrito
remove the * from the delete statement.

should be like this:

Code: Select all

delete from myTable where ...

Posted: Fri Sep 29, 2006 2:28 pm
by volka
And don't change the id. Make it an auto_increment field and leave it alone.