Hybride Posted August 22, 2007 Share Posted August 22, 2007 This page shows all the users in the database and the type they are (admin/user). Basically what am trying to do is update based on the type (admin/user) for a single user... and I know I need to have my UPDATE query have a WHERE statement, however if I try to update with a where, it won't work. Am also trying to do this all on one page, if at all possible. I know it's an extremely easy thing to do, but I can't figure it out. <div class="center" style="width:500px; position:absolute; top:0px; left:300px;"> <form action="<?php echo $PHP_SELF; ?>" method="post" > <fieldset><legend><b>Users</b></legend> <? echo '<table style="position:absolute;"><tr class="subhead"><td>USERNAME</td><td>TYPE</td><td>UPDATE</td></tr>'; $query = "SELECT * FROM users WHERE id >='2' ORDER BY username ASC"; $result = mysql_query($query) or die(mysql_error()); if($result) { while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo '<td>' . $row['username'] . '</td>'; echo '<td>' . $row['type'] . '</td>'; echo '<td><select name="type"><option value="admin">Admin</option><option value="user">User</option></select></td></tr>'; } echo '<input type="submit" name="update" value="Update"><input type="hidden" name="update" value="TRUE">'; } if(isset($_POST['update'])) { $uquery = "UPDATE users SET type='$type'"; $uresult = mysql_query($uquery) OR die(mysql_error()); if($uresult) { echo 'Update successful!'; } else { echo 'Update unsuccessful. ' . mysql_error(); } } ?> </table></fieldset></form> Also, another problem I've been having is how do I convert spaces in a search to '+'? Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/66095-solved-stupid-update-problem/ Share on other sites More sharing options...
keeB Posted August 22, 2007 Share Posted August 22, 2007 Can you give me an example of the UPDATE that's not working? From there we can better address your issue. Quote Link to comment https://forums.phpfreaks.com/topic/66095-solved-stupid-update-problem/#findComment-330551 Share on other sites More sharing options...
Ken2k7 Posted August 22, 2007 Share Posted August 22, 2007 The variable $type doesn't contain anything in that code. <?php $sql = "UPDATE table SET column_name=value WHERE column_name=admin/user"; mysql_query($sql) or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/66095-solved-stupid-update-problem/#findComment-330556 Share on other sites More sharing options...
keeB Posted August 22, 2007 Share Posted August 22, 2007 Not necessarily, and that doesn't really answer his Q. Quote Link to comment https://forums.phpfreaks.com/topic/66095-solved-stupid-update-problem/#findComment-330558 Share on other sites More sharing options...
Hybride Posted August 22, 2007 Author Share Posted August 22, 2007 Can you give me an example of the UPDATE that's not working? From there we can better address your issue. The update works, however, it updates *all* of the users, when I just need the selected one to change. (It uses a select/option for that). Should I just have the update manually be typed in? Quote Link to comment https://forums.phpfreaks.com/topic/66095-solved-stupid-update-problem/#findComment-331210 Share on other sites More sharing options...
Hybride Posted August 23, 2007 Author Share Posted August 23, 2007 I figured how to do the checkbox problem, however now I have this weird thing going on where the Select/Option will update only the first option for the records, but allows the last record to have the option of changing both status. Ie: Record 1: name / admin / > updates only on admin option Record 2: name / user / > updates only on admin option Record 3: name / admin / > updates on either admin or user option What's even weirder is that Records 1 and 2 update if you select the *3rd* record's option. I know it has something to do with the for() loop thing, but am lost how to fix it. <? echo '<table><tr class="subhead"><td>USERNAME</td><td>TYPE</td><td>ADMIN/USER</td></tr>'; $query = "SELECT * FROM users WHERE id >='2' ORDER BY username ASC"; $result = mysql_query($query) or die(mysql_error()); $count=mysql_num_rows($result); $delete = $_POST['delete']; $checkbox = $_POST['checkbox']; $type= $_POST['type']; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo '<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['id'] . '">'; echo $row['username'] . '</td>'; echo '<td>' . $row['type'] . '</td>'; echo '<td><select name="type"><option name="type" value="admin">Admin</option><option name="type" value="user">User</option></select></td></tr>'; } echo '<tr><td><input type="submit" name="update" id="update" value="Update"></td></tr>'; if($_POST['update']){ for($i=0;$i<$count;$i++){ $u_id = $checkbox[$i]; $sql = "UPDATE users SET type='$type' WHERE id='$u_id'"; $result2 = mysql_query($sql) OR die(mysql_error()); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66095-solved-stupid-update-problem/#findComment-332382 Share on other sites More sharing options...
thefollower Posted August 23, 2007 Share Posted August 23, 2007 this may not help but what you should do which is what i do is ... by default new accounts = user. and to avoid hackers etc. better to "manually" change user in the player's field to admin when needs be. im assuming it doesnt happen non stop so it shouldn't cause alot of trouble. Plus on top of that... it would seem a safer method in my opinion than a script to do it for you cos it could be exploited. Quote Link to comment https://forums.phpfreaks.com/topic/66095-solved-stupid-update-problem/#findComment-332385 Share on other sites More sharing options...
Hybride Posted August 24, 2007 Author Share Posted August 24, 2007 Yeah you have a point. Thanks for the help/advice. Quote Link to comment https://forums.phpfreaks.com/topic/66095-solved-stupid-update-problem/#findComment-333364 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.