View Single Post
  #1 (permalink)  
Old April 20th, 2005, 10:36 AM
scoobie scoobie is offline
Authorized User
 
Join Date: Jan 2005
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ho to do nested if statements

Hi,

i was wondering if anyone can help me. i have created a form that allows a user to change their email address. when they enter in their old, new and confirm email address and click submit i want the php to perform certain checks such as if the address is in the valid format or if the new email already exists in the database. The problem is these checks are within and if and when i run the code it prints all the echo statements instead of just the section that failed. Here is the code i used.

if ($ModUname)
{
if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $OldUNameFrm, $NewUNameFrm, $ConfUNameFrm))
{
echo "You Must Enter A Valid Email Address.<br>Please Try Again";
}
/*CHECK IF NEW EMAIL EXISTS IN THE DATABASE */
$check = mysql_query("select Username from acctbl where Username = '$NewUNameFrm'");
$result = mysql_query($check);
$num = mysql_num_rows($check);
if ($num > 0)
{
echo "This Email Address Already Exists. Please Try Again";
}
/* CHECK IF NEW AND CONFIRM MATCH */
if ($NewUNameFrm != $ConfUNameFrm)
{
echo "Email Address Don't Match. Please Re-Enter and Confirm Email.";
}
else{
mysql_query("UPDATE acctbl SET Username = '$_POST[NewUNameFrm]'
where Username = '$_POST[OldUNameFrm]'");
echo "Your Email Has Been Successfully Updated";
}
}//end if
else{?
<form method="post" action="<?php echo $PHP_SELF?>">
<b>Change Email</b>
Old Email<input type = "text" name = OldUNameFrm size = "30">
New Email<input type = "text" name = NewUNameFrm size = "30">
Confirm New Email<input type = "text" name = ConfUNameFrm size = "30">
<input type = "Submit" name = "ModUname" value = "Change Email">
</form>
<?php
}//end else
?>

if i put in else if / elseif in the first if statement then the code doesn't run.

can anyone see where i am going wrong?

Thanks in advanced

scoobie

Reply With Quote