EMail address changes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

if noone can help fix the code, does any know where to look?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You can fix the code. Here's the logic.
  1. User opens a form with their information in it.
  2. User changes their information and submits it.
  3. The script checks to make sure the email address submitted is not already in the database belonging to someone else.
  4. If the email address exists for someone else, error out.
  5. If the email address does not exist, update the user record and complete the script.
See if you can translate that into code. Post back with problems.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

4Boredom, you have the email address of the user when you populate their information.... so use that as $users_current_email


So before you run your query.... do this

Code: Select all

if ($users_current_email == $users_changed_email){
// update all information
}else{
//check if email exists
}

if ($users_changed_email exists){ 
// throw error
}else{
//update
}
note: this is just the logic, not the actual code.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

This first is the edited and the second is the orig.. I think im changing it wrong?

Code: Select all

if ($users_current_email == $users_changed_email){ 
				$sql = mysql_query("UPDATE `users` SET email_address = '{$email_address}' WHERE userid = '{$userid}' ");
                $sql = mysql_query("UPDATE `basic` SET gender = '($gender)' WHERE userid = '($userid)'");
				$sql = mysql_query("UPDATE `basic` SET month = '($month)' WHERE userid = '($userid)'");
				$sql = mysql_query("UPDATE `basic` SET day = '($day)' WHERE userid = '($userid)'");
				$sql = mysql_query("UPDATE `basic` SET year = '($year)' WHERE userid = '($userid)'"); 

		if(!$sql){ 
    echo 'There has been an error creating your account. Please contact the webmaster.'; 
    echo mysql_error();
} else { 
    echo 'E-Mail updated! This is your new login, so remember it! '.$email_address.''; 
}
}else{ 
 $sql_email_check = mysql_query("SELECT username FROM users WHERE email_address='$email_address'");
$email_check = mysql_num_rows($sql_email_check);
$check = mysql_fetch_array($sql_email_check);
if($check['user']!=$username){
echo "Please fix the following errors: <br />"; 
echo "<strong>Your email address has already been used by another member in our database. Please use a different email address!<br />"; 
exit(); } 
} 

if ($users_changed_email exists){ 
// throw error 
}else{ 
//update 
}

Code: Select all

if ($users_current_email == $users_changed_email){ 
// update all information 
}else{ 
//check if email exists 
} 

if ($users_changed_email exists){ 
// throw error 
}else{ 
//update 
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"exists" isn't a keyword. Maybe you're thinking of isset() or empty()?
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

i had no idea that was just his suggestion and i was tryna make it work.. I wanted to see what people thought b4 i overwrite my code
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

I have to declare the following before I import form post variables right?

Code: Select all

$email_address = $users_current_email
What do I do for $users_changed_email

do I do $email_address = $users_changed_email after the posting of the form?

Will this sequence work?
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

upping this
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, let's see if we can cover this a bit better.

When you post the form, take the posted value of the users email address they want to update to and check to see if that is in the table BUT NOT associated with the user...

Code: Select all

<?php
if (isset($_POST['check_some_filed_other_than_submit']))
{
    // Form was posted 

    // Edit this post var to be the same name as the form field for user_email
    $user_email = $_POST['email_form_field'];

    // Edit this post var to be the same name as the form field for user_name
    $user_name = $_POST['username_form_field']; 

    // Change the field names to be the what you need them to be
    $sql = "SELECT COUNT(`user_id`) AS check_count 
            FROM `user_table` 
            WHERE `user_email` = '$user_email' 
            AND `user_name` != '$user_name'";
    if (!$result = mysql_query($sql))
    {
         die('Could not get count to check email against: ' . mysql_error());
    }

    // Use the query return value ...
    while ($row = mysql_fetch_array($result))
    {
        $check_count = $row['check_count'];
    }

    // ...to check if that email exists in the database already
    if ($check_count <> 0)
    {
        // If we get here then the supplied email address exists
        // in the database for some other persons record.
        // That means we need to error out of the script somehow
        // Pick your poison, for now I am just going to die
        die('The email address you entered is already is use in the system, please enter another one');
    }
    else
    {
        // If we are here, then the email address does not exist in the system
        // But it could already be in there for this user, so we need to check 
        // that, and if so, handle it appropriately

        // But this is up to you so take care of that on your own 
    }
}
?>
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

Ive been trying this several times and this is still greek to me... Please use the variables I post when you explain this... remember im a php beginner

This simply needs to allow them to click update without changing their email address because it is included on a form with other options that let them update their profile. So They can keep their current but cant duplicate an email address of someone else in the DB... someone please help with the issue this time not just with a generic answer... ive been stuck in a standstill for weeks

Code: Select all

# grab the POST variables from the HTML form
$email_address = $_POST['email_address']; 

# Any escaped characters?
$email_address = stripslashes($email_address);

# does this user already exist in the database?  

 $sql_email_check = mysql_query("SELECT username FROM users WHERE email_address='$email_address'");
$email_check = mysql_num_rows($sql_email_check);
$check = mysql_fetch_array($sql_email_check);
if($check['user']!=$username){
echo "Please fix the following errors: <br />"; 
echo "<strong>Your email address has already been used by another member in our database. Please use a different email address!<br />"; 
exit(); }
Post Reply