Delete A Specific User.


I saw Dean's Delete Unactive Users but what if you wanted to delete a specific user?
So I thought what the hell I'll make a code, and here it is.

Okay no mysql queries so straight into the code.

Save as delete_user.php
PHP Code
  1. <?php
  2. session_start();
  3. include "config.php";
  4. if($logged[username] && $logged[userlevel] == 6)
  5. {
  6. switch($_GET[page])
  7. {
  8. default:
  9. $fetch = mysql_query("SELECT * FROM `members`");
  10. while ($delete = mysql_fetch_array($fetch))
  11. {
  12. echo ("- <a href='?page=verifydelete&username=$delete[username]'>$delete[username]</a><br>");
  13. }
  14. break;
  15.  
  16. case 'verifydelete':
  17. echo ("Are you sure you want to delete $_GET[username]?
  18. <br><br>
  19. <a href='?page=delete&username=$_GET[username]'>Yes</a> - <a href='delete_user.php'>No</a>");
  20. break;
  21.  
  22. case 'delete':
  23. $username = strip_tags(htmlspecialchars($_GET[username]));
  24. $delete = mysql_query("DELETE FROM `members` WHERE username = '$username'");
  25. echo ("User Deleted.<br><a href='delete_user.php'>Go back.</a>");
  26. break;
  27. }
  28. }
  29. else
  30. {
  31. echo ("You must be admin to view this page.");
  32. }
  33. ?>

What do these codes mean? Take the time to read on and learn.

PHP Code
  1. <?php
  2. session_start();
  3. include "config.php";
  4. if($logged[username] && $logged[userlevel] == 6)
  5. {

Our all important header. Starts our session and makes sure that only the people with level 6 are allowed to view this page.

PHP Code
  1. $fetch = mysql_query("SELECT * FROM `members`");
  2. while ($delete = mysql_fetch_array($fetch))
  3. {
  4. echo ("- <a href='?page=verifydelete&username=$delete[username]'>$delete[username]</a><br>");
  5. }

It displays all of our members check out my tutorial in the Mysql category to see the explanation of the while tag.

PHP Code
  1. $username = strip_tags(htmlspecialchars($_GET[username]));
  2. $delete = mysql_query("DELETE FROM `members` WHERE username = '$username'");
  3. echo ("User Deleted.<br><a href='delete_user.php'>Go back.</a>");

Deletes our user from the database. And displays a message that our user is now deleted.

hope you liek this code.
SkillMaster's Avatar
Views:
2,419
Rating:
Posted on Tuesday 8th April 2008 at 05:24 PM
Dalez
Dalez's Avatar
Thanks, this is very useful!
Posted on Thursday 28th June 2007 at 08:39 PM
SkillMaster
SkillMaster's Avatar
0.o Dan giving me kudos. ~Feels all happy inside~

Thanks Dan(Y)
Posted on Thursday 28th June 2007 at 08:37 PM
DanielXP
DanielXP's Avatar
Nice tutorial. Also like the confirmation that you want to delete the user.