Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 20th, 2005, 10:36 AM
Authorized User
Join Date: Jan 2005
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ho to do nested if statements


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 = 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";
if ($NewUNameFrm != $ConfUNameFrm)
echo "Email Address Don't Match. Please Re-Enter and Confirm Email.";
mysql_query("UPDATE acctbl SET Username = '$_POST[NewUNameFrm]'
where Username = '$_POST[OldUNameFrm]'");
echo "Your Email Has Been Successfully Updated";
}//end if
<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">
}//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


Reply With Quote
  #2 (permalink)  
Old April 21st, 2005, 11:39 AM
Authorized User
Join Date: Jan 2005
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts

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


Reply With Quote
  #3 (permalink)  
Old April 24th, 2005, 06:22 AM
Friend of Wrox
Join Date: Mar 2004
Location: Bhopal, MP, India.
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul

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.

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 07:38 PM.

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