query("SELECT email FROM user_account WHERE username !='$username' AND email = '$newEmail'");
if($result->num_rows > 0){ //email already exist
$msg = "Try Again!!
This email is already registered to another account!!";
$pg_email = 1;
$msg_type = 1;
}
else{ //update new email into database
$result = $conn->query("UPDATE user_account SET email= '$newEmail' WHERE username = '$username'");
if($result){
$msg = "Your email has been Updated!";
//we also update the email of session variable to a new one
$_SESSION['email'] = $newEmail;
$pg_email = 0;
$msg_type = 2;
}
else{
$msg = "Error updating record: " . $conn->error;
$msg_type = 1;
}
// generate 'random code'
function getRandomCode() {
$n=10;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomCode = '';
for ($i = 0; $i < $n; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomCode .= $characters[$index];
}
return $randomCode;
}
// check if 'Verify-Code' exist
$result = $conn->query("SELECT verifyCode FROM user_account WHERE username = '$username'");
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$verifyCode = $row["verifyCode"];
}
}
else{ //generate code
$verifyCode = getRandomCode();
$conn->query("UPDATE user_account SET verifyCode = '$verifyCode' WHERE username = '$username'");
}
if ( isset($verifyCode) ){
//Now after updating the email we email the verify code again
//Send it to his new email
$to = $newEmail;
$subject = "Account-Verification Code For GEESE";
$message = "
Greenscape Environmental Experience & Learning Survey Experiments