tags:

views:

38

answers:

1

hi there

i am building an edit account form, where the user can update or edit his/her account setting such as: username, password, and email

check the submitted email and compare it to the database
1st condition: if this email is the same with the previous one
   leave it be
2nd condition: but if this email is different that the previous one
   check for other emails in the user's table
   3rd condition: if the same email found 
      return false and suggest to use another one since this one is already taken
   4th condition: if the email is available to use
      update the new email based on the user's id in the database

and here's my code: (edited from the last one)

$found_member = Member::find_by_id($session->user_id);
$members = Member::find_all();

$found_member = Member::find_by_id($session->user_id);
if($_POST['password'] == $found_member->pass_unmd){
    if($_POST['email'] == $found_member->email){
        //do the regular update
    }elseif($_POST['email'] !== $found_member->email){
        $found_another = Member::check_email_availability($_POST['email']);
        if(!empty($found_another->email)){
            //the email is taken
        }else{
            //the email is updated using the new one
        }
    }
}

am i on the right track ?? since i get a feeling that this is not the best thing to do, please give me advise

Edit Note: i edit the code based on @Vinko's suggestion and because after i tested my previous code, it doesnt work :D

thank you

A: 

Create a unique index on the email column. If you attempt to update or insert a record that violates the unique constraint, the database will trigger an error.

Phil Brown