Jump to content

Nested If - Else not working on this piece of code.


TheFreak

Recommended Posts

I am trying to edit the details of a user present in a database by searching his username in the database first (my first If - Else condition,if i dont find the username in the database fine,else onto my second nested if - else) and then editing it.When i input a username and it finds it in the database it gives me option to edit it,but when i press "Edit" it again reaches to the search username form,instead of printing the username.Please help

 

if(isset($_POST['submit']))
     {
        
          $username= clean($_POST['username']); 
          $check="SELECT * FROM users WHERE username = '$username'";
       $check1=mysql_query($check) or die("Could not get category address");
       $check2 = mysql_num_rows($check1);  
          if ($check2 == 0) 
         {
       print "<br>UserName not present,please choose another."; 
        }
       else
         { 
            if(isset($_POST['edit']))
              {
                print $username;  
               //This is just to check if the output is right or not,ill put UPDATE sql query here.
              }
              else
              {
                   print "<br><br>Please fill in the details to add a user.<br>";  ?>
        <form id="Form" name="Form" method="POST" action="">
        <table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
        <tr>
        <td width="112"><b><br>UserName</b></td>
        <td width="188"><br><input name="username" type="text" class="textfield" id="username" /></td>
        </tr>
        <tr>
        <td width="112"><b><br>Password</b></td>
        <td width="188"><br><input name="password" type="text" class="textfield" id="password" /></td>
        </tr>
        <tr>
        <tr>
        <td width="112"><b><br>Email</b></td>
        <td width="188"><br><input name="email" type="text" class="textfield" id="email" /></td>
        </tr>
        <td> </td>
       <td><br><input type="submit" name="edit" value="Edit" /></td>
      </tr>
      </table>
      </form>
<?php  print "</td></tr></table>";
              }
         }
   
     }
     else
     {  ?>
         <form id="Form" name="Form" method="POST" action="">
        <table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
        <tr>
        <td width="112"><b><br>UserName</b></td>
        <td width="188"><br><input name="username" type="text" class="textfield" id="username" /></td>
        </tr>   
         <td> </td>
       <td><br><input type="submit" name="submit" value="submit" /></td>
      </tr>   </table>
      </form> 
       <?php } ?>   

When you push your edit button the page refreshes and when you do that you reach the username form because the the $_POST['submit'] will be unset. And since you have the edit form inside the isset($_POST['submit']) - if-statment you would never get to it.

 

Try this instead:

 

<?php

if (isset($_POST['submit'])) {

    // Check username and try to find it

} elseif (isset($_POST['edit'])) {

    // Edit user

} else {

    // View the username form

}

When you push your edit button the page refreshes and when you do that you reach the username form because the the $_POST['submit'] will be unset. And since you have the edit form inside the isset($_POST['submit']) - if-statment you would never get to it.

 

Try this instead:

 

<?php

if (isset($_POST['submit'])) {

    // Check username and try to find it

} elseif (isset($_POST['edit'])) {

    // Edit user

} else {

    // View the username form

}

 

 

Well its again going to the first else i.e showing the search username field

 

Here is my changed code

 

if(isset($_POST['submit']))
     {
        
          $username= clean($_POST['username']); 
          $check="SELECT * FROM users WHERE username = '$username'";
       $check1=mysql_query($check) or die("Could not get category address");
       $check2 = mysql_num_rows($check1);  
          if ($check2 == 0) 
         {
       print "<br>UserName not present,please choose another."; 
        }
        elseif(isset($_POST['edit']))
              {
                print $username;  
              }
              else
              {
                   print "<br><br>Please fill in the details to add a user.<br>";  ?>
        <form id="Form" name="Form" method="POST" action="">
        <table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
        <tr>
        <td width="112"><b><br>UserName</b></td>
        <td width="188"><br><input name="username" type="text" class="textfield" id="username" /></td>
        </tr>
        <tr>
        <td width="112"><b><br>Password</b></td>
        <td width="188"><br><input name="password" type="text" class="textfield" id="password" /></td>
        </tr>
        <td> </td>
       <td><br><input type="submit" name="edit" value="Edit" /></td>
      </tr>
      </table>
      </form>
<?php  print "</td></tr></table>";
              }
         }
   
   
     else
     {  ?>
         <form id="Form" name="Form" method="POST" action="">
        <table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
        <tr>
        <td width="112"><b><br>UserName</b></td>
        <td width="188"><br><input name="username" type="text" class="textfield" id="username" /></td>
        </tr>   
         <td> </td>
       <td><br><input type="submit" name="submit" value="submit" /></td>
      </tr>   </table>
      </form> 
       <?php } ?>   

Try this:

<?php

if (isset($_POST['submit'])) {

    // Check username and try to find it

   if (ok) {
      // view edit user form
   } else {
       // view errors and username form
   }

} 

if (isset($_POST['edit'])) {

    // Check the information entered in edit user form
   if (ok) {

        // update user
   } else {

       // Show errors and edit user form again

   }
      

} else {

    // View the username form

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.