Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/66095-solved-stupid-update-problem/
Share on other sites

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?

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());
	}
}

?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.