staples27 Posted July 28, 2009 Share Posted July 28, 2009 I have a form that is meant to edit username, levels, etc when i hit sutbmit but it's not working. The form was previously created by someone else and I've been trying to tweak to get it to work but no success... The code is as follows: include 'admindbc.php'; $file_name = "admin"; include("headera.php"); if($_SESSION['user_level'] == "0"){ echo "Only administrators are allowed here!"; echo "<br><br>"; echo "<a href='../index.php'>Home</a>"; exit(); } include 'admindbc.php'; if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") { if (!isset($_POST["submit"])) { $useridb = $_GET["id"]; $sql = "SELECT * FROM users WHERE id=$useridb"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> <div id="maincontent3"> <h1>Edit Profile</h1> <br> <form action="edit.php" method="post"> <input type="hidden" name="cmd" value="edit"> <input type=hidden name="id" value="<?php echo $myrow["id"] ?>"> <table> <tr> <td><b>User ID:</b></td> <td><?php echo $myrow["id"] ?></td> </tr> <tr> <td><b>Username:</b></td> <td> <INPUT NAME="user_name" VALUE="<?php echo $myrow["user_name"] ?>" SIZE=30></font></td> </tr> <tr> <td><b>Activated: </b></td> <td> <input class="box" type="checkbox" name="activated" value="1" <?php if($myrow['activated'] == 1){ echo "checked"; } ?>> </font></td> </tr> <tr> <td><b>User Level: </b></td> <td><select size="1" name="user_level"> <option <?php if($myrow['user_level'] == 0){ echo "selected"; } ?>>0</option> <option <?php if($myrow['user_level'] == 1){ echo "selected"; } ?>>1</option> <option <?php if($myrow['user_level'] == 2){ echo "selected"; } ?>>2</option> </select></font></td> </tr> <tr> <td> </td> <td> <input type="submit" name="submit" value="Change" class="button"></font></td> </tr> </table> </form> <p><a href="admin_users.php">Go Back</a></p> <? include 'admindbc.php'; } ?> <? if ($_POST["submit"]) { $usernameb = $_POST['user_name']; if (!$_POST['activated']) { $activatedb = "0"; } else { $activatedb = $_POST['activated']; } $user_levelb = $_POST['user_level']; $sql = "UPDATE users SET user_name='$usernameb', activated='$activatedb',user_level='$user_levelb' WHERE id=$useridb"; $result = mysql_query($sql); echo "<div id=\"maincontent3\">Thank you! Information updated."; echo "<br><br>"; echo "<a href='index.php'>Admin Main Menu</a> | <a href='admin_users.php'> Manage Users</a></div>"; } } ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/ Share on other sites More sharing options...
MadTechie Posted July 28, 2009 Share Posted July 28, 2009 saying "doesn't work" won't get you many replies except for maybe what do you mean ? So.. what do you mean ? I did a quick review (not knowing what to look for but did noticed that it probably doesn't update, this is due to the fact your pulling the data via get (seams fine) but when posting your not passing the id via get but their is one in POST but you don't use it, so maybe try! if ($_POST["submit"]) { $usernameb = $_POST['user_name']; $useridb = $_POST['id']; //Add Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-884786 Share on other sites More sharing options...
staples27 Posted July 28, 2009 Author Share Posted July 28, 2009 Sorry I should've been clearer but yeah the form isn't updating the database when i submit it... I've just tried passing the id in POST but still no luck Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-884791 Share on other sites More sharing options...
patrickmvi Posted July 28, 2009 Share Posted July 28, 2009 There seems to be a variaty of issues with the script. One thing I can see is the fact that you're including admindbc.php over and over. Also, where does $useridb in your update statement? Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-884794 Share on other sites More sharing options...
MadTechie Posted July 28, 2009 Share Posted July 28, 2009 What about if you reload the page, does it update.. the reason i ask is because your displaying the details before doing the update heres a quick clean up (also i noticed an extra } at the end, so I added one at the start.. (so you may want to remove the <?php { at the start) <?php { include 'admindbc.php'; $file_name = "admin"; include("headera.php"); if($_SESSION['user_level'] == "0"){ echo "Only administrators are allowed here!"; echo "<br><br>"; echo "<a href='../index.php'>Home</a>"; exit(); } include 'admindbc.php'; if ($_POST["submit"]) { $usernameb = $_POST['user_name']; if (!$_POST['activated']) { $activatedb = "0"; }else{ $activatedb = "1"; } $user_levelb = (int)$_POST['user_level']; $sql = "UPDATE users SET user_name='$usernameb', activated='$activatedb',user_level='$user_levelb' WHERE id=$useridb"; $result = mysql_query($sql); } if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") { if (!isset($_GET["id"])) { $useridb = (int)$_GET["id"]; $sql = "SELECT * FROM users WHERE id=$useridb"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> <div id="maincontent3"> <h1>Edit Profile</h1> <br> <form action="edit.php" method="post"> <input type="hidden" name="cmd" value="edit"> <input type=hidden name="id" value="<?php echo $myrow["id"] ?>"> <table> <tr> <td><b>User ID:</b></td> <td><?php echo $myrow["id"] ?></td> </tr> <tr> <td><b>Username:</b></td> <td> <INPUT NAME="user_name" VALUE="<?php echo $myrow["user_name"] ?>" SIZE=30></font></td> </tr> <tr> <td><b>Activated: </b></td> <td> <input class="box" type="checkbox" name="activated" value="1" <?php if($myrow['activated'] == 1) echo "checked"; ?> > </font></td> </tr> <tr> <td><b>User Level: </b></td> <td><select size="1" name="user_level"> <option <?php if($myrow['user_level'] == 0) echo "selected";?> >0</option> <option <?php if($myrow['user_level'] == 1) echo "selected";?> >1</option> <option <?php if($myrow['user_level'] == 2) echo "selected";?> >2</option> </select></font></td> </tr> <tr> <td> </td> <td> <input type="submit" name="submit" value="Change" class="button"></font></td> </tr> </table> </form> <p><a href="admin_users.php">Go Back</a></p> <?php include 'admindbc.php'; } echo "<div id=\"maincontent3\">Thank you! Information updated."; echo "<br><br>"; echo "<a href='index.php'>Admin Main Menu</a> | <a href='admin_users.php'> Manage Users</a></div>"; } } ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-884800 Share on other sites More sharing options...
staples27 Posted July 28, 2009 Author Share Posted July 28, 2009 MadTechie - I've tried what you've posted but still no luck, this time it skips the whole form and just heads straight to the echo after it's submitted... The reason why I'm displaying the details before the update it is so that the user can view the info of the selected entity that needs to be edited/updated if that makes sense? Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-884925 Share on other sites More sharing options...
MadTechie Posted July 28, 2009 Share Posted July 28, 2009 change if (!isset($_GET["id"])) back to if (isset($_GET["id"])) Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-884930 Share on other sites More sharing options...
staples27 Posted July 28, 2009 Author Share Posted July 28, 2009 still no luck, no update on the database, although this time it displays the form but also the echo at the same time Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-885001 Share on other sites More sharing options...
MadTechie Posted July 28, 2009 Share Posted July 28, 2009 add some debugging, as theirs too many thing that could be cause this, is the query being called does it produce and error etc Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-885074 Share on other sites More sharing options...
diondev Posted July 28, 2009 Share Posted July 28, 2009 Replace your PHP under the form with this: <?php if (isset($_POST["submit"])) { $usernameb = $_POST['user_name']; if (!$_POST['activated']) { $activatedb = "0"; } else { $activatedb = $_POST['activated']; } $user_levelb = $_POST['user_level']; $sql = "UPDATE users SET user_name='$usernameb', activated='$activatedb',user_level='$user_levelb' WHERE id=$useridb"; mysql_query($sql) or die(mysql_error()); echo "<div id=\"maincontent3\">Thank you! Information updated."; echo "<br><br>"; echo "<a href='index.php'>Admin Main Menu</a> | <a href='admin_users.php'> Manage Users</a></div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-885154 Share on other sites More sharing options...
staples27 Posted July 28, 2009 Author Share Posted July 28, 2009 well after that i get the following: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 also with error reporting enabled i'm getting notices for undefined index Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-885168 Share on other sites More sharing options...
MadTechie Posted July 28, 2009 Share Posted July 28, 2009 well after that i get the following: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 you may want to echo the query to check it, also with error reporting enabled i'm getting notices for undefined index Well that means they haven't been set! Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-885181 Share on other sites More sharing options...
staples27 Posted July 29, 2009 Author Share Posted July 29, 2009 problem sovled it was largely down to the index/variables not being set and also had to change $sql to mysql_query for the update script thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-885774 Share on other sites More sharing options...
MadTechie Posted July 29, 2009 Share Posted July 29, 2009 good feeling when you fix something like that can you click the topic solved button (bottom left) Quote Link to comment https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/#findComment-885781 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.