Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 20th, 2005, 10:36 AM
Authorized User
 
Join Date: Jan 2005
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

 
Old April 21st, 2005, 11:39 AM
Authorized User
 
Join Date: Jan 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's ok i sorted it. i just took the select query and placed it before the if statement and it works perfectly now.

scoobie

 
Old April 24th, 2005, 06:22 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

Ok, u sorted out.
I may say, if there is only a single statement in if or for loop, skipping of curly braces, helps to reduce code-space.
( Besides use of classes, inclues, requires lieu structural functions )

Also switch case branching is better than many if statements.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Mulitiple/Nested IIF statements rohit_ghosh Access VBA 3 June 1st, 2007 10:44 AM
Ho do we store pictures in a database? vitalis PHP Databases 2 March 18th, 2007 12:05 PM
Ho to write Macro??? keepsmiling_sruti Excel VBA 1 July 22nd, 2005 07:45 AM
Plz Help - Nested SQL Statements? esemerda SQL Language 2 February 28th, 2005 06:41 AM
how i use the Stored Procedure return value and ho yoord ADO.NET 5 June 28th, 2004 08:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.