开发者

php update password or save default if it is blank

Can someone point me out how can I save the default password if it is blank? Its an update issue. Im making an update page when the user update their profile. Its a long profile info. I did not post it all here coz my only problem is the password field. Even if it is leave as 开发者_如何学运维blank it still updating the field on the database.I use md5 for encryption. Below is the code. Please just add the code, your code. Thank you.

The id is=1 because im just testing it. Ill only have one data in the userstest table.

    $desire= $_POST['desired'];//username field
    $password = md5(trim(mysql_prep($_POST['password'])));//password field
    $passconfirm = md5(trim(mysql_prep($_POST['confirmpassword']))); //confirmpasswor field
    $sql = mysql_query("UPDATE userstest SET username = '$desire',password='$password',confirmpassword='$passconfirm' WHERE id=1");
    if(mysql_affected_rows()==1){
    echo "Update Successfull";
    }else{
    echo "Update Failed" . mysql_error();
    }


Add a if condition while building query

$sql = "UPDATE userstest SET username = '$desire'"; 
if($password) {$sql += ",password='$password',confirmpassword='$passconfirm'";}
$sql += " WHERE id=1";

then run the query mysql_query($sql);


Don't update the password or confirmpassword columns if those fields are blank. Just don't add them to the SQL query in that case.

By the way, why are you even saving the confirmpassword? Shouldn't this always be the same as password? It's usually only used in an if statement in the PHP script to see that the user didn't do a typo.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜