Forums

Delete user form

FenFire 20 Apr, 2015
Hello again,

I'm trying to create a form for a "delete user" action. I want to delete user entries in all relevant tables and delete the profile picture. So I tried a custom code (in PHP tags):
mysql_query("DELETE FROM kwm_users WHERE id = ". $form->data['id'] ."");
mysql_query("DELETE FROM kwm_arrange2drive WHERE user_id = ". $form->data['id'] ."");
mysql_query("DELETE FROM kwm_a2d_user WHERE user_id = ". $form->data['id'] ."");
mysql_query("DELETE FROM kwm_a2d_drivealert WHERE user_id = ". $form->data['id'] ."");
mysql_query("DELETE FROM kwm_car_offers WHERE user_id = ". $form->data['id'] ."");
mysql_query("DELETE FROM kwm_gadgets WHERE user_id = ". $form->data['id'] ."");
mysql_query("DELETE FROM kwm_contact_details WHERE name = ". $form->data['name'] ."");
  unlink( $_SERVER['DOCUMENT_ROOT']."/mobil3/components/com_arrange2drive/media/users/{$form->data['avatar']}" );


Well, it doesn't work... Do I have to connect to the DB in the custom code? I have a DB read action in the onload event...

Moreover, I want the user to be logged out after deleting his profile. How can I achieve that?

Thanks in advance.

Christian
GreyHead 21 Apr, 2015
1 Likes
Hi Christian,

Please try using the Joomla! DB code:
$db = JFactory::getDBO();
$query = "
  DELETE 
    FROM `#__arrange2drive`
    WHERE `user_id` = '{$form->data['id']}';
";
$db->setQuery($query);
$db->execute();
. . .

Be very careful deleting records from the #__users table directly - this may well leave all kinds of un-linked elsewhere in the Joomla! tables. Better check if there is a JUser method to do this and use that.

You can't log out a deleted user so I think you'd need to do that before deleting them.

Bob.
FenFire 21 Apr, 2015
Answer
Oh sorry, I meant the user should be logged out after "clicking the button". I looked up how to log out myself.
Thanks for your help, it works now. Here's the code (think it could be done better with array and a loop, but it's fine like this).

<?php
$mainframe = JFactory::getApplication();
$userid = $form->data['id'];
$name = $form->data['name'];
$avatar = $form->data['avatar'];
$mainframe->login($userid);

$db = JFactory::getDBO();
$query = "DELETE FROM `#__users` WHERE `id` = '$userid';";
$db->setQuery($query);
$db->execute();

$query = "DELETE FROM `#__a2d_user` WHERE `user_id` = '$userid';";
$db->setQuery($query);
$db->execute();

$query = "DELETE FROM `#__a2d_arrange2drive` WHERE `user_id` = '$userid';";
$db->setQuery($query);
$db->execute();

$query = "DELETE FROM `#__a2d_drivealert` WHERE `user_id` = '$userid';";
$db->setQuery($query);
$db->execute();

$query = "DELETE FROM `#__car_offers` WHERE `user_id` = '$userid';";
$db->setQuery($query);
$db->execute();

$query = "DELETE FROM `#__gadgets` WHERE `user_id` = '$userid';";
$db->setQuery($query);
$db->execute();

$query = "DELETE FROM `#__contact_details` WHERE `name` = '$name';";
$db->setQuery($query);
$db->execute();

  unlink( JPATH_SITE."/components/com_arrange2drive/media/users/{$form->data['avatar']}" );
?>


Thanks,
Christian
This topic is locked and no more replies can be posted.