If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > A question about nested 'If' statements

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-29-11, 23:20
Serien Serien is offline
Registered User
 
Join Date: May 2011
Posts: 4
A question about nested 'If' statements

Hello!

I am currently writing a script that I hope will allow users on my website to change/modify their account information. I am using a mysql database to store the information and forms to change it out.

The script works fine if a single change is made, but if I try to change all three fields at once, only the first 'if' statement executes and only the Username is changed.

I would like users to be able to change only one field, or any combination of the three if they so choose, and so don't want to nest all of my if statements inside of one another. Is there any way to run multiple if statements side by side?

Here is my code
PHP Code:
$newusername $_POST["new_username"]; 
           
$newpassword $_POST["new_password"];
           
$newpassword2 $_POST["new_password2"];
           
$newemail $_POST["new_e-mail"];
           
$currentpassword $_POST["current_password"];
           
           if (
$currentpassword == $data['Password'])
           {
                if (
$newusername != NULL)
                {
                    
mysql_query ("UPDATE User_Info SET Username = '$newusername' WHERE Username = '$User'");
                    
$_SESSION['User']=$newusername;
                }
                if (
$newpassword != NULL)
                {
                    if (
$newpassword == $newpassword2)
                    {
                        
mysql_query ("UPDATE User_Info SET Password = '$newpassword' WHERE Username = '$User'");
                    }
                    else
                    {
                        echo 
"<p>The two passwords that you entered do not match.</p>";
                    }
                }
                if (
$newemail != NULL)
                {
                    
mysql_query ("UPDATE User_Info SET E-mail = '$newemail' WHERE Username = '$User'");
                }
                echo 
"<p>Your chosen profile information has been successfully updated</p>";
           }
           else
           {
            echo 
"<p>I'm sorry, profile information change unsuccessful.  The current password that you have entered is incorrect.</p>";
           } 
Reply With Quote
  #2 (permalink)  
Old 05-30-11, 06:56
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
not the following is 'air code', not tried, tested, proven working, heck its not even be complete. in may include typos and so on.....
PHP Code:
$updateStatement =  "";
if (
$currentpassword != $data['Password']) 
{  echo 
"<p>The two passwords that you entered do not match.</p>"
}  else
{  if (
$newusername != NULL
   { 
$updateStatement "Username = '$newusername'";
   }
   if (
$newpassword != NULL
   {  if (
strlen($updateStatement)>5)
     {  
$updateStatement .= ", ";
     }
       
$updateStatement " Password = '$newpassword'";
     } 
   } 
   if (
$newemail != NULL
   {  if (
strlen($updateStatement)>5)
     {  
$updateStatement .= ", ";
     }
       
$updateStatement " E-mail = '$newemail'";
     } 
   }
   if (
strlen($updateStatement)>5)  //do we have any changes to post
   
{  $sql "UPDATE mytable SET $updateStatement WHERE Username = '$User';"
   } 
   
//Insert your code to run the $sql
   //check the update worked (check the MySQL_errno()
   //if it worked
   
echo "<p>Your chosen profile information has been successfully updated</p>"
   
//else it didn't
   
echo "<p>Your chosen profile information wasn't updated, becuase MySQL got all girly and whinged:-".$mysql_error."</p>"

__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 05-30-11, 15:02
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
As an aside:
What prevents a user from changing his userid to that of another valid user?
Reply With Quote
  #4 (permalink)  
Old 06-14-11, 13:56
Serien Serien is offline
Registered User
 
Join Date: May 2011
Posts: 4
Hey!

Sorry it took so long to respond ^^.

As far as the identical username issue goes, I still have to get that coded ^^. Thanks for the heads up though!

It worked pretty well once I finally got the chance to mess with it! I would have never thought to combine them all in a single variable, very brilliant move on your part =D. Guess that thinking comes with more experience, hehe.

I did have to change a few of the lines a bit to fit the situation, but this code works perfectly. Thanks so much!

PHP Code:
           $newusername mysql_escape_string($_POST["new_username"]); 
           
$newpassword mysql_escape_string($_POST["new_password"]);
           
$newpassword2 mysql_escape_string($_POST["new_password2"]);
           
$newemail mysql_escape_string($_POST["new_e-mail"]);
           
$newprofile mysql_escape_string($_POST["new_profile"]);
           
$currentpassword mysql_escape_string($_POST["current_password"]);
           
           if (
$currentpassword != $data['Password'])
           {
                echo 
"<p>I'm sorry, profile information change unsuccessful.  The current password that you have entered is incorrect.</p>";
           }
           else
           {
                
$updateStatement "";
                if (
$newusername != NULL)  
                {
                    
$updateStatement "Username = '$newusername'";
                    
$_SESSION['User']=$newusername;
                } 
                if (
$newpassword != NULL)  
                {
                    if (
strlen($updateStatement)>5
                    {
                        
$updateStatement .= ", "
                    } 
                    
$updateStatement .= " Password = '$newpassword'"
                }  
                if (
$newemail != NULL)  
                {
                    if (
strlen($updateStatement)>5
                    {
                        
$updateStatement .= ", "
                    } 
                
$updateStatement .= " Email = '$newemail'"
                }  
                if (
strlen($updateStatement)>5)  //do we have any changes to post 
                
{
                    
$sql "UPDATE User_info SET $updateStatement, Profile = '$newprofile' WHERE Username = '$User';";  
                }
                else
                {
                    
$sql "UPDATE User_info SET Profile = '$newprofile' WHERE Username = '$User';"
                }
                
mysql_query ($sql);
                if (!
mysql_errno()) //checks for mysql error
                
{
                    echo 
"<p>Your chosen profile information has been successfully updated</p>";  
                }
                else
                {
                    
$mysql_error mysql_error();
                    echo 
"<p>Your chosen profile information wasn't updated, becuase MySQL got all girly and whinged:-".$mysql_error."</p>";  
                }
           } 

Last edited by Serien; 06-14-11 at 14:07.
Reply With Quote
Reply

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 On
Pingbacks are On
Refbacks are On