David Garcia
David Garcia

Reputation: 2696

PHP IF condition advise needed

I'm using emails as username to login into a site being developed, now if a user updates their email from the profile page, how can i make sure that my email checking statement doesnt catch the user's email as already registered in the database.

the page profile

 /* Now we will store the values submitted by form in variable */
           $fullname=$_POST['fullname'];
           $dob=$_POST['dob'];
           $address=$_POST['address'];
           $myusername=$_POST['myusername'];           
           $telephone=$_POST['telephone'];

$queryuser=mysql_query("SELECT * FROM customer WHERE email='$myusername' ");
$checkuser=mysql_num_rows($queryuser);

if($checkuser != 0) 
{ 
$Merr[]='» Sorry this email is already registered!';
}
else {$insert_user=mysql_query("UPDATE CUSTOMER SET SYNTAX HERE");

Now these are the fields in question;

(name, dob, address, email, telephone) VALUES ('$fullname', '$dob', '$address', '$myusername', '$telephone')

As you can see if the user changes the login email then the syntax checks for the email being submitted against the database, however if the user leaves the email unchanged he will get the error because it is found in the database.

I was thinking of something like;

if($checkuser != 0) {
  if($myusername == $_POST['myusername'])
   (...dont show error.) 

but my php skills are limited. can anyone advise please, thanks

Upvotes: 1

Views: 116

Answers (3)

Joel
Joel

Reputation: 577

/* Now we will store the values submitted by form in variable */
$myusername=$_POST['myusername'];           


$queryuser=mysql_query("SELECT username FROM customer WHERE email='$myusername' ");
$checkuser=mysql_num_rows($queryuser);

/*Mysql_fetch_row will select the first row from the query and put it into a 
multidimensional array. As you only want 1 e-mail associated with one user the query
should only ever select one row anyway.  */
$row = mysql_fetch_row($queryuser);

if($checkuser != 0) 
{ 
      if($row[0] != $SESSION['username'])
      {
      //ERROR
      }

}
else {
//Update details
}

That way you can just submit the e-mail along with any other inputs and if the e-mail hasn't changed you wont need to do anything.

Also you've said your posting the logged in users username. I would imagine you would actually want to be using sessions to do that.

And don't forget to escape the POST data for security.

Also the INSERT INTO should that not be an update statement if they are updating their e-mail address?

Upvotes: 1

MiniduM
MiniduM

Reputation: 176

Do you keep an id sort of thing for the users? If so you could check for the existing email addresses that is not the current users email address.

Upvotes: 0

Anil
Anil

Reputation: 588

If you are updating the database use Update.. Update customers set email='$username' where email='$username'.

you can't use the same form for registering and updating information.

Upvotes: 0

Related Questions