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
https://forums.phpfreaks.com/topic/168970-condense-query/
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
https://forums.phpfreaks.com/topic/168970-condense-query/#findComment-891500
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.