Results 1 to 4 of 4
  1. #1
    Join Date
    May 2011
    Posts
    4

    Unanswered: 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>";
               } 

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    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 on the Tiger 800 or the Norton

  3. #3
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    As an aside:
    What prevents a user from changing his userid to that of another valid user?

  4. #4
    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 15:07.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •