How do I change this script so when it finds a user exists in a MySQL database it updates and doesn't return the error message?

StackOverflow https://stackoverflow.com/questions/23564631

  •  18-07-2023
  •  | 
  •  

Question

This part of the script checks for multiple usernames, I want it to update the user found instead of ending with the user exists error message:

// Verify a users existance by username.  Function will fail if multiple usernames are encountered.
function userExists( $username ) 
{
    // set up prepared statement
    $stmt = $this->conn->prepare("SELECT * FROM $this->table WHERE $this->username_column = ?");

    // bind the parameters
    $stmt->bind_param("s", $username);

        // execute prepared statement 
    if(!$stmt->execute())
        {
            $this->lastError = "Error in ".__FUNCTION__.": Username provided has more then one (".$stmt->num_rows().") records associated with it where there should only be one record.";
            return false;
        }


    $stmt->store_result();

        // check to make sure a username doesn't have duplicate usernames
        if($stmt->num_rows() == 0)
        {
            $this->lastError = "Error in ".__FUNCTION__.": Username provided has more then one (".$stmt->num_rows().") records associated with it where there should only be one record.";
            return false;           
        }

        // return indicating success 
        return true; 
}

I'm not sure if this is needed to aid in the answer but this part of the script is used to add new users to the database:

function addUser( $username, $password ) 
{

    // encrypt password if set (default == enabled w/ sha1)
    if($this->use_encryption)
        $password = ($_SESSION['password']);
        $salt = "CHANGE-SALT"; 
        // Add some salt to the users password. 
        $salt .= $password; // The password is salted
        $password = $salt; // Change the password var to contain our new salted pass. 
        $password = md5($password);

    if($this->userExists( $username ))  
        die("ERROR USER EXISTS"); 


    // create sql   
    $sql = "INSERT INTO  $this->table ( $this->username_column , $this->password_column) VALUES (?, ?)";

    // set up prepared statement
    $stmt = $this->conn->prepare($sql);

    // bind the parameters
    $stmt->bind_param("ss", $username, $password);


     // execute prepared statement 
    if(!$stmt->execute())
        {
        $this->lastError = "Error in ".__FUNCTION__.": Supplied user/pass fields cannot be added.";
        return false;       
        }



    return true;
}       

Any help at all with this would be greatly appreciated, thanks in advance.

Trevor

Was it helpful?

Solution

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

Add Unique index to the username column;

REPLACE INTO user SET username=".$username." AND password=".$password;

http://dev.mysql.com/doc/refman/5.5/en/replace.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top