user3531070
user3531070

Reputation: 1

PHP change email address on account when logged in

I am trying to enable a user to change their password on their account when logged in. However, I cant seem to get it to work.

Do you have any suggestions?

<?php

if ($_POST['submitEmail'])
{

 $newemail = $_POST['email'];
 $newemail = stripslashes($newemail);
 $newemail = str_replace("'", "&#39", "$newemail");

  //checks database to see if email user types in already exists
 $query = "SELECT * FROM users WHERE email = '$newemail'";
$result = mysqli_query($db_connection, $query);
$nums = mysqli_num_rows($result);

 if ($nums >= 1)
 {
  //if email already exists, inform user
 echo "Email already exists";
 echo "<br/>Click <a href = 'account.php?page=email'> HERE</a> to try again";
 }

else
{

//if email does not already exist, update users email
   $querychange = "UPDATE users SET email = '$newemail' where id = '$userID'"; 
   $result3 = mysqli_query($db_connection, $querychange); 

   echo "Your Email has been changed";
   }

}


  else {

  echo "<strong> Current Email: </strong>$email ";
  ?>

    <!-- Allows users to enter new email address -->
    <form name="changeEmail" id="changeEmail" method="post" action="account.php?page=email">
        <input type="hidden" value="email" name="account_submit_type"/>
        <input type='hidden' name='changeEmail' value='yes'>
        <strong> Email </strong><input type = "text" name = "email" size="40" value=""> <br>
        <input type ="button" value="submitEmail" onclick="verifyForm()"/>
    </form>
<?php
}
?>

Upvotes: 0

Views: 569

Answers (1)

xate
xate

Reputation: 6379

<input type ="button" value="submitEmail" onclick="verifyForm()"/>

This has to be

<input type="submit" value="submitEmail"/>

Another error: Undefined $email:

echo "<strong> Current Email: </strong>"; echo $email;

has to be like this:

echo "<strong> Current Email: </strong>"; echo $_POST['email'];

Full version which works for me:

<?php

if ($_POST['submitEmail'])
{

    $newemail = $_POST['email'];
    $newemail = stripslashes($newemail);
    $newemail = str_replace("'", "&#39", "$newemail");

    //checks database to see if email user types in already exists
    $query = "SELECT * FROM users WHERE email = '$newemail'";
    $result = mysqli_query($db_connection, $query);
    $nums = mysqli_num_rows($result);

    if ($nums >= 1)
    {
        //if email already exists, inform user
        echo "Email already exists";
        echo "<br/>Click <a href = 'account.php?page=email'> HERE</a> to try again";
    }

    else
    {

        //if email does not already exist, update users email
        $querychange = "UPDATE users SET email = '$newemail' where id = '$userID'";
        $result3 = mysqli_query($db_connection, $querychange);

        echo "Your Email has been changed";
    }

}


else {

    echo "<strong> Current Email: </strong>"; echo $_POST['email'];
    ?>

    <!-- Allows users to enter new email address -->
    <form name="changeEmail" id="changeEmail" method="post" action="#?page=email">
        <input type="hidden" value="email" name="account_submit_type"/>
        <input type='hidden' name='changeEmail' value='yes'>
        <strong> Email </strong><input type = "text" name = "email" size="40" value=""> <br>
        <input type="submit" value="submitEmail"/>
    </form>
    <?php
}
?>

Upvotes: 1

Related Questions