Jump to content

condense query


rondog

Recommended Posts

I have a function to delete a user. I also want to delete all of the users information in other tables. The way I am doing it right now works fine, but I was wondering if I could condense it into 1 query?

if ($_GET['action'] == 'delete')
{
$id			= $_GET['id'];
$deleteUser = mysql_query("DELETE FROM users WHERE id = '".$id."'") or die(mysql_error());
if ($deleteUser)
{
	$deleteProgress = mysql_query("DELETE FROM progress WHERE userid = '".$id."'") or die(mysql_error());
	if ($deleteProgress)
	{
		$status = "Successfully deleted the selected user.";
	}
}
else 
{
	$status = "Failed to delete the selected user!";
}
}

Link to comment
Share on other sites

Just as an aside, do you realize you have an unhandled error in that logic? If the first query succeeds and the second does not, there is no response given. If you decide that you will continue with the current process of deleting records from each table individually, I would suggest reversing the process. Delete the associated records and then the parent record. Otherwise, if there is a problem, you would have orphaned records.

 

Example:

if ($_GET['action'] == 'delete')
{
   $id = mysql_real_escape_string($_GET['id']);
   $deleteProgress = mysql_query("DELETE FROM progress WHERE userid = '".$id."'") or die(mysql_error());
   if ($deleteProgress)
   {
      $deleteUser = mysql_query("DELETE FROM users WHERE id = '".$id."'") or die(mysql_error());
   }

   if ($deleteProgress && $deleteUser)
   {
      $status = "Successfully deleted the selected user.";
   }
   else 
   {
      $status = "Failed to delete the selected user!";
   }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.