delete user account PhP

Discussion in 'PHP' started by newbie12345, May 12, 2010.

  1. #1
    hey guys im getting the account to delete only if i Put the correct user name and Password

    but suppose the user enters a wrong user name and Password to delete an account
    i want a message to disPlay saying wrong username or Password.
    i tried this but im not getting it to echo the message "wrong username or password "if the user enter a wrong user name and Password

    my code

    
    <?php
    include ('connect.php');
    
    if (isset($_POST["submit"])){
    
          $user  = $_POST['u_name'];
          $pass  = $_POST['pwd'];
    	  
    if(!$user){
    echo "please enter your username. <br>";
    }
    if(!$pass){
    	echo "please enter your password";
    	header('Refresh: 9; deleteform.php');
    	}
    if ($user && $pass){
    	   $sql= ("DELETE FROM user_accounts WHERE username = '$user' AND password = '$pass' ");
    	   $res = mysql_query($sql);
    	   echo"your account has been deleted";
    	   } ELSE {
    		   echo"invalid username and password";
    	  } 
    		  
    }
    ?>
    
    PHP:

     
    newbie12345, May 12, 2010 IP
  2. easic

    easic Active Member

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    70
    #2
    - Well, first of all, you need to sanitize your $_POST. See This. (for security purposes)

    - You can check if that DELETE query was sucessfully by calling "mysql_affected_rows()" function.
    If it returns 1, then the username and password were correct. Otherwise, invalid.

    
    ...
    $res = mysql_query($sql);
         if (mysql_affected_rows() == 1)  echo "your account has been deleted";
          else echo "invalid username and password";
    
    Code (markup):
    - Also, you can put 'limit 1' at the end of the sql, i don't know how your database is structured, but that little change could increase speed.

    
     $sql= ("DELETE FROM user_accounts WHERE username = '$user' AND password = '$pass'  LIMIT 1");
    
    Code (markup):
     
    easic, May 12, 2010 IP
  3. newbie12345

    newbie12345 Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    tnx worked perfectly
     
    newbie12345, May 12, 2010 IP