TheFreak Posted March 20, 2009 Share Posted March 20, 2009 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 } ?> Link to comment https://forums.phpfreaks.com/topic/150282-nested-if-else-not-working-on-this-piece-of-code/ Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 you need exit after the second if to stop it doing any think elese. <?php exit; ?> Link to comment https://forums.phpfreaks.com/topic/150282-nested-if-else-not-working-on-this-piece-of-code/#findComment-789203 Share on other sites More sharing options...
Kalland Posted March 20, 2009 Share Posted March 20, 2009 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 } Link to comment https://forums.phpfreaks.com/topic/150282-nested-if-else-not-working-on-this-piece-of-code/#findComment-789208 Share on other sites More sharing options...
TheFreak Posted March 20, 2009 Author Share Posted March 20, 2009 you need exit after the second if to stop it doing any think elese. <?php exit; ?> But i want to update the user details in the second if Link to comment https://forums.phpfreaks.com/topic/150282-nested-if-else-not-working-on-this-piece-of-code/#findComment-789220 Share on other sites More sharing options...
TheFreak Posted March 20, 2009 Author Share Posted March 20, 2009 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 } ?> Link to comment https://forums.phpfreaks.com/topic/150282-nested-if-else-not-working-on-this-piece-of-code/#findComment-789222 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 try <?php if (isset($_POST['submit'])) { // Check username and try to find it } if (isset($_POST['edit'])) { // Edit user } else { // View the username form } Link to comment https://forums.phpfreaks.com/topic/150282-nested-if-else-not-working-on-this-piece-of-code/#findComment-789224 Share on other sites More sharing options...
TheFreak Posted March 20, 2009 Author Share Posted March 20, 2009 try <?php if (isset($_POST['submit'])) { // Check username and try to find it } if (isset($_POST['edit'])) { // Edit user } else { // View the username form } No it doesn't work. Link to comment https://forums.phpfreaks.com/topic/150282-nested-if-else-not-working-on-this-piece-of-code/#findComment-789267 Share on other sites More sharing options...
Kalland Posted March 20, 2009 Share Posted March 20, 2009 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 } ?> Link to comment https://forums.phpfreaks.com/topic/150282-nested-if-else-not-working-on-this-piece-of-code/#findComment-789279 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.