Jump to content

Admin Edit User's Information


Smudly

Recommended Posts

Having a few problems with my Admin Page that lists all registered users with some of their information.

I have a Delete button (which deletes the user permanently) and a Ban button (which bans user until I unban them). Both of these buttons have their own javascript that asks for a confirmation before going through with the request of banning or deleting a user. The confirm window show up correctly, with the option to click OK, or Cancel. When I click OK, it deletes or bans the user successfully, however if I hit cancel, it does not stop the script. It deletes or bans the user. That's the first problem.

 

The next:

 

At the end of the page I have a Submit button. So for example, I change any of the user's information on the current page, I can hit submit, and update all the user's information in the database. I am unsure how to set this up, because it needs to go through each row and update them one at a time as to not give all users the same updated value.

 

I also need to create a Dropdown Menu, or input field, which allows me to choose a number (50, 100, 300, 500, etc) of users to show per page. I have no idea how to go about doing this.

 

<?php
session_start();
include_once('../inc/connect.php');

if(!isset($_SESSION['sort_counter']))
{$_SESSION['sort_counter'] = 1;}

if(($_SESSION['sort_counter']%2) == 0){ //test even value
  $sortcount = "DESC";
}else{ //odd value
  $sortcount = "";
}

// $result = mysql_query("SELECT * FROM users ORDER BY id");  ORIGINAL
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.id"); 
// $result = mysql_query("SELECT * FROM users JOIN users ON userstats.id = userstats.id ORDER BY id");

$today = date("Y-m-d");

$sort = $_GET['sort'];
$delete = $_GET['delete'];
$ban = $_GET['ban'];
$submit = $_POST['submit'];

if ($sort=='id'){ 

// $result = mysql_query("SELECT * FROM users ORDER BY id");  
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.id $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run

}
if ($sort=='username'){ 

// $result = mysql_query("SELECT * FROM users ORDER BY username"); 
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.username $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='email'){ 

// $result = mysql_query("SELECT * FROM users ORDER BY email"); 
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.email $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='type'){ 

// $result = mysql_query("SELECT * FROM users ORDER BY member"); 
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.member $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='referrer'){ 

// $result = mysql_query("SELECT * FROM users ORDER BY referrer"); 
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.referrer $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='level'){ 

// $result = mysql_query("SELECT * FROM userstats ORDER BY level"); 
$result = mysql_query("SELECT * FROM userstats LEFT JOIN users ON users.id = userstats.id ORDER BY userstats.level $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='exp'){ 

// $result = mysql_query("SELECT * FROM userstats ORDER BY exp"); 
$result = mysql_query("SELECT * FROM userstats LEFT JOIN users ON users.id = userstats.id ORDER BY userstats.exp $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='credits'){ 

// $result = mysql_query("SELECT * FROM userstats ORDER BY credits"); 
$result = mysql_query("SELECT * FROM userstats LEFT JOIN users ON users.id = userstats.id ORDER BY userstats.credits $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}

if ($delete && isset($_GET['id']))
{
    mysql_query('DELETE FROM users WHERE id = ' . (int)$_GET['id']);
mysql_query('DELETE FROM userstats WHERE id = ' . (int)$_GET['id']);
echo "<SCRIPT language='JavaScript'><!--
  window.location='users.php';//-->
</SCRIPT>";
}  

if ($ban=="true" && isset($_GET['id']))
{
    mysql_query('UPDATE `users` SET `active`="no" WHERE id = ' . (int)$_GET['id']);
echo "<SCRIPT language='JavaScript'><!--
  window.location='users.php';//-->
</SCRIPT>";
}  
if ($ban=="false" && isset($_GET['id']))
{
    mysql_query('UPDATE `users` SET `active`="yes" WHERE id = ' . (int)$_GET['id']);
echo "<SCRIPT language='JavaScript'><!--
  window.location='users.php';//-->
</SCRIPT>";
}  

// head
echo "
<html>
<head>
<title>Users</title>
<style>
a:link{
text-decoration: none;
color: #519904;
}
a:visited{
text-decoration: none;
color: #519904;
}
a:hover{
text-decoration: none;
color: #4296ce;
}
#joined{
position: relative;
width: 97px;
margin-left: auto;
margin-right: auto;
top: -550px;
}
</style>
</head>
<body>
";

echo "<h2 align='center'>Users</h2><br /><table border='1' align='center'>
<tr>
<th bgcolor='#cccccc'><a href='users.php?sort=id'>ID</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=username'>Username</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=email'>Email</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=type'>Type</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=referrer'>Referrer</a></th>

<!-- Level, Exp, and Credits are in the table called userstats -->

<th bgcolor='#cccccc'><a href='users.php?sort=level'>Level</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=exp'>Exp</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=credits'>Credits</a></th>
<th bgcolor='#cccccc'><a href='users.php'>Delete</a></th>
<th bgcolor='#cccccc'><a href='users.php'>Ban</a></th>
</tr><form>";
echo "<script type='text/javascript'>
function show_ban()
{
var r=confirm('Ban?');
if (r==true)
  {
  // Ban
  }
else
  {
  // Don't ban
  }
}
</script>";

echo "<script type='text/javascript'>
function show_unban()
{
var r=confirm('Unban?');
if (r==true)
  {
  // Unban
  }
else
  {
  // Don't Unban
  }
}
</script>";

echo "<script type='text/javascript'>
function show_delete()
{
var r=confirm('Delete?');
if (r==true)
  {
  // Delete
  }
else
  {
  // Don't delete
  }
}
</script>";

$recentmembers = 0;
while($row = mysql_fetch_array($result))
  {
  $joined = $row['joindate'];
  if ($joined==$today){
  $recentmembers += 1;
  }
  $active = $row['active'];
  $color = "#ffffff";
  $banned = "Ban";
  if ($active=='no'){
  $color = "#f43636";
  $banned = "Unban";
  $active = "false";
  $alert = "show_unban";
  }
  else{
  $active = "true";
  $alert = "show_ban";
  }
  if ($row['member'] == 1){
  $typecolor = "#72A4D2";
  }
  if ($row['member'] == 0){
  $typecolor = "#eeeeee";
  }
  if ($row['member'] == 9){
  $typecolor = "#00cc00";
  }
  
  
  echo "<tr>";
  echo "<td align='center' width='40' bgcolor='$color'>" .$row['id']. "</td>";
  echo "<td align='center' width='130'><input type='text' name='username' value='" .$row['username']. "'></td>";
  echo "<td align='center' width='230'><input type='text' name='email' value='" .$row['email']. "' size='35'></td>";
  echo "<td align='center' width='10'><input type='text' name='member' value='" .$row['member']. "' size='2' style='background-color: $typecolor'></td>";
  echo "<td align='center' width='130'><input type='text' name='referrer' value='" .$row['referrer']. "'></td>";
  echo "<td align='center' width='10'><input type='text' name='level' value='" .$row['level']. "' size='2'></td>";
  echo "<td align='center' width='10'><input type='text' name='exp' value='" .$row['exp']. "' size='10'></td>";
  echo "<td align='center' width='10'><input type='text' name='credits' value='" .$row['credits']. "' size='20'></td>";
  echo "<td align='center' width='10'><a href='users.php?delete=true&id=" .$row['id']. "' onclick='show_delete()'>Delete</a></td>";

  echo "<td align='center' width='10'><a href='users.php?ban=$active&id=" .$row['id']. "' onclick='$alert()'>$banned</a></td>";
  echo "</tr>";
  }

echo "</table><br /><center><input type='submit' name='submit' value='Submit Changes'><input type='reset' name='reset' value='Reset'></form></center>";
echo "<br /><div id='joined'>Joined Today: ".$recentmembers."</div>";

// Footer
echo "
</body>
</html>
";

// Change User's Information

if (isset($submit)){

// UPDATE USERS INFORMATION FOR ONLY THE ROWS THAT HAVE BEEN MODIFIED


}

?>

 

Any input appreciated.

 

Thanks for those who keep the help coming!

Link to comment
https://forums.phpfreaks.com/topic/208116-admin-edit-users-information/
Share on other sites

Ok here is some information that should help you. I fixed alot of your code to be alot easier to read and did this to help you so that you may in the future use these methods. I added in the ability to only delete an ban users if yes is selected, and I also added in the ability for you to be able to loop through and update the users. I have it printing out the array at this time but you should be able to loop through that data and insert what is necessary. The last one I left up to you to at least take an attempt at it. One thing of help I can give you for that is that you can use what is called pagination (google it). What I would do is set a page through the url or the session and then you can limit the query using the LIMIT keyword. Please at least attempt it and I might be willing to help further. Please let me know if you have any issues with my code.

 

<?php
session_start();
include_once('../inc/connect.php');

//set the sort counter
if(!isset($_SESSION['sort_counter'])) $_SESSION['sort_counter'] = 1;

//get the sort
if(($_SESSION['sort_counter']%2) == 0){
  $sortcount = "DESC";
} else {
  $sortcount = "ASC";
}

//start the query
$query = "SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY "; 

//get the date
$today = date("Y-m-d");

//get values
$sort 	= $_GET['sort'];
$delete 	= $_GET['delete'];
$ban 	= $_GET['ban'];
$submit 	= $_POST['submit'];

//check for post update
if(isset($submit)){
//print the array
print_r($_POST);
}

//get the sort type
switch($sort){
case 'username':
	$query .= " users.username";
	$_SESSION['sort_counter'] += 1;
	break;
case 'email':
	$query .= " users.email";
	$_SESSION['sort_counter'] += 1;
	break;
case 'type':
	$query .= " users.member";
	$_SESSION['sort_counter'] += 1;
	break;
case 'referrer':
	$query .= " users.referrer";
	$_SESSION['sort_counter'] += 1;
	break;
case 'level':
	$query .= " userstats.level";	
	$_SESSION['sort_counter'] += 1;
	break;
case 'exp':
	$query .= " userstats.exp";
	$_SESSION['sort_counter'] += 1;
	break;
case 'credits':
	$query .= " userstats.credits";
	$_SESSION['sort_counter'] += 1;
	break;
default:
	$query .= "users.id";
	$_SESSION['sort_counter'] += 1;
	break;
}

//add the sort type
$query .= " $sortcount";

//check if delete is set and id is numeric
if (isset($delete) && is_numeric($_GET['id'])){
//delete the user and the user stats (dont need (int) because we made sure it was numeric)
mysql_query('DELETE FROM users WHERE id='.$_GET['id']);
mysql_query('DELETE FROM userstats WHERE id='.$_GET['id']);

//redirect
header("Location:users.php");
}  

//check if ban is set to true and id is numeric
if ($ban=="true" && is_numeric($_GET['id'])){
//ban the user
mysql_query('UPDATE users SET active="no" WHERE id='.$_GET['id']);

//redirect
header("Location:users.php");
}  

//check if ban is set to false and id is numeric
if ($ban=="false" && isset($_GET['id'])){
    	//unban the user
   	mysql_query('UPDATE users SET active="yes" WHERE id=',$_GET['id']);

//redirect
header("Location:users.php");
}  
?>
<html>
<head>
<title>Users</title>
<style>
a:link{
	text-decoration: none;
	color: #519904;
}
a:visited{
	text-decoration: none;
	color: #519904;
}
a:hover{
	text-decoration: none;
	color: #4296ce;
}
#joined{
	position: relative;
	width: 97px;
	margin-left: auto;
	margin-right: auto;
	top: -550px;
}
</style>
</head>
<body>

<h2 align='center'>Users</h2>
<br />
<table border='1' align='center'>
<tr>
	<th bgcolor='#cccccc'><a href='users.php?sort=id'>ID</a></th>
	<th bgcolor='#cccccc'><a href='users.php?sort=username'>Username</a></th>
	<th bgcolor='#cccccc'><a href='users.php?sort=email'>Email</a></th>
	<th bgcolor='#cccccc'><a href='users.php?sort=type'>Type</a></th>
	<th bgcolor='#cccccc'><a href='users.php?sort=referrer'>Referrer</a></th>

	<!-- Level, Exp, and Credits are in the table called userstats -->

	<th bgcolor='#cccccc'><a href='users.php?sort=level'>Level</a></th>
	<th bgcolor='#cccccc'><a href='users.php?sort=exp'>Exp</a></th>
	<th bgcolor='#cccccc'><a href='users.php?sort=credits'>Credits</a></th>
	<th bgcolor='#cccccc'><a href='users.php'>Delete</a></th>
	<th bgcolor='#cccccc'><a href='users.php'>Ban</a></th>
</tr>

<form>
<?php
//make the query
$results = mysql_query($query);

//initialize recent members
$recentmembers = 0;

//loop through the results
while($row = mysql_fetch_array($result)){
//get options
$joined = $row['joindate'];
$active = $row['active'];
  	
//add recent members
if ($joined==$today){
  		$recentmembers += 1;
  	}

//set options based on ban
if ($active=='no'){
  		$color = "#f43636";
  		$active = false;
	$ban = "true";
} else {
	$color = "#ffffff";  
  		$active = true;
	$ban = "false";
  	}
  	
switch($row['member']){
	case 0:
		$typecolor = "#eeeeee";
		break;
	case 1:
		$typecolor = "#72A4D2";
		break;
	case 9:
		$typecolor = "#00cc00";
		break;
}
?>
<tr>
	<input type="hidden" name="user_id[]" value="<?php echo $row['id'];?>" />
	<td align="center" width="40" bgcolor="<?php echo $color;?>"><?php echo $row['id'];?></td>
	<td align="center" width="130"><input type="text" name="username[]" value="<?php echo $row['username'];?>"></td>
	<td align="center" width="230"><input type="text" name="email[]" value="<?php echo $row['email'];?>" size="35"></td>
	<td align="center" width="10"><input type="text" name="member[]" value="<?php echo $row['member'];?>" size="2" style="background-color: <?php echo $typecolor; ?>;"></td>
	<td align="center" width="130"><input type="text" name="referrer[]" value="<?php echo $row['referrer'];?>"></td>
	<td align="center" width="10"><input type="text" name="level[]" value="<?php echo $row['level'];?>" size="2"></td>
	<td align="center" width="10"><input type="text" name="exp[]" value="<?php echo $row['exp'];?>" size="10"></td>
	<td align="center" width="10"><input type="text" name="credits[]" value="<?php echo $row['credits'];?>" size="20"></td>
	<td align="center" width="10"><a href="javascript:void(0);" onclick="show_delete('<?php echo $row['id'];?>');">Delete</a></td>
	<td align="center" width="10"><a href="javascript:void(0);" onclick="show_ban('<?php echo $ban;?>','<?php echo $row['id'];?>');"><?php echo ($active==true) ? "Ban" : "Unban";?></a></td>
</tr>
<?php
} //end while
?>
</table>
<br />
<center><input type="submit" name="submit" value="Submit Changes"><input type="reset" name="reset" value="Reset"></center>
</form>
<br />
<div id="joined">Joined Today: <?php echo $recentmembers;?></div>

</body>

<script type="text/javascript">
function show_delete(user_id){
	var answer = confirm("Delete User?");
	if(answer){
		window.location = 'users.php?delete&id='+user_id;
	}
}

function show_ban(ban,user_id){
	var ban_type = (ban=="true") ? 'Ban' : 'Unban';
	var answer = confirm(ban_type+' user?');
	if(answer){
		window.location = 'users.php?ban='+ban+'&id='+user_id;
	}
}
</script>
</html>

Thanks for the tips. It helped a lot!

 

I'm currently working on updating the user's information after hitting submit. After looking through your code, I was unsure how I'm supposed to get the variables for each user and update them into the database. After hitting submit, something like the following is passed into the url:

 

users.php?username=admi1234423&email=smudly01231224%40gmail.com&member=0&referrer=&level=1&exp=0&credits=50.000&username=teamfortress&email=teamfortress%40yahoo.com&member=0&referrer=&level=1&exp=0&credits=50.000&submit=Submit+Changes

 

So using this, how would I take the first user's values and set them into variables, then pass them to the database, and do the same thing for the next user, and so on?

 

Thanks

These are called GET parameters.

 

For the URL you provided, you would call the user name like this:

 

$user = $_GET['username'];

 

And $user would be this: admi1234423

 

I suggest cleaning the variables before putting them into the db... mysql_real_escape_string or addslashes will do the trick...

http://us3.php.net/manual/en/function.mysql-real-escape-string.php

http://us3.php.net/manual/en/function.addslashes.php

Sorry about that, I missed something. You should change the form to have a (method="post") so that they are passed in a POST instead of a GET. I added a hidden field called user_id which is the users id. The data posts all the users data like you wanted to but it posts all the data as a multi-dimensional array. Which means that it will look like something this:

 

$_POST[0]['user_id'] = 1;
$_POST[0]['username'] = username here;
$_POST[1]['user_id'] = 2
$_POST[1]['username'] = username here;

 

so with that data what you can do is perform an update query on each of the rows to update the users information while you perform a loop. It will look something like this:

 

for($i=0;$i<count($_POST);$i++){
     $update_query = "UPDATE users SET username='".mysql_real_escape_string($_POST[$i]['username'])."' WHERE id=".(int)$_POST[$i]['user_id'];
     mysql_query($update_query);
}

 

The important part in that is the WHERE section. The id is how you are telling it which username belongs to which user in the database. You would have to also tell it to set all of the other values but I think you can get a general sensus of what to do with the data. Before you start running queries, you can try setting up the loop and just outputting the data to the page to ensure that you are going to update the database correctly.

Alright, I'm trying to understand this new concept best I can.

 

I fixed the form to now show: action='users.php' method='POST'

I've tried testing it by typing in the for loop code you provided, and instead of updating the database, I tried echoing out this:

 

echo ($_POST[$i]['username']);

 

Nothing shows (no errors either). I think I'm doing this wrong. How do I test this correctly? I'm not sure how the syntax works.

at the very top of the page do a:

 

print_r($_POST);

 

if you are unfamiliar with this what it does is it prints out the value of the array to the page. Make sure you submit the form before doing this or it will not have any values - Then right click the page somewhere and do view page source. You should be able to then see the values. Make sure that you are seeing the values there, then the problem lies somewhere in your loop....can you post your new code?

Alright, I placed the print_r statement at the top of my page. Upon hitting submit, on the top of the page, the following show up:

 

Array ( [username] => cookiemonster => [email protected] => 0 [referrer] => [level] => 1 [exp] => 0 [credits] => 50.000 [submit] => Submit Changes )

 

 

This is the last user that is listed on my page and all his information.

 

So this part seems to work correctly. Now I just need to fix my For Loop. *see end of my code*.

 

<?php
session_start();
include_once('../inc/connect.php');

if(!isset($_SESSION['sort_counter']))
{$_SESSION['sort_counter'] = 1;}

if(($_SESSION['sort_counter']%2) == 0){ //test even value
  $sortcount = "DESC";
}else{ //odd value
  $sortcount = "";
}

$recentmembers = 0;
$today = date("Y-m-d");
print_r($_POST);

// $result = mysql_query("SELECT * FROM users ORDER BY id");  ORIGINAL
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.id"); 
// $result = mysql_query("SELECT * FROM users JOIN users ON userstats.id = userstats.id ORDER BY id");

$membertype = mysql_query("SELECT member FROM users WHERE member='1'");
$typecount = mysql_num_rows($membertype);

$freetype = mysql_query("SELECT member FROM users WHERE member='0'");
$freecount = mysql_num_rows($freetype);

$joindatequery = mysql_query("SELECT joindate FROM users WHERE joindate='$today'");
$joindateresult = mysql_num_rows($joindatequery);

$referquery = mysql_query("SELECT referrer FROM users WHERE referrer=''");
$referresult = mysql_num_rows($referquery);

$sort = $_GET['sort'];
$delete = $_GET['delete'];
$ban = $_GET['ban'];
$submit = $_POST['submit'];

if ($sort=='id'){ 

// $result = mysql_query("SELECT * FROM users ORDER BY id");  
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.id $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run

}
if ($sort=='username'){ 

// $result = mysql_query("SELECT * FROM users ORDER BY username"); 
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.username $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='email'){ 

// $result = mysql_query("SELECT * FROM users ORDER BY email"); 
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.email $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='type'){ 

// $result = mysql_query("SELECT * FROM users ORDER BY member"); 
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.member $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='referrer'){ 

// $result = mysql_query("SELECT * FROM users ORDER BY referrer"); 
$result = mysql_query("SELECT * FROM users LEFT JOIN userstats ON userstats.id = users.id ORDER BY users.referrer $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='level'){ 

// $result = mysql_query("SELECT * FROM userstats ORDER BY level"); 
$result = mysql_query("SELECT * FROM userstats LEFT JOIN users ON users.id = userstats.id ORDER BY userstats.level $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='exp'){ 

// $result = mysql_query("SELECT * FROM userstats ORDER BY exp"); 
$result = mysql_query("SELECT * FROM userstats LEFT JOIN users ON users.id = userstats.id ORDER BY userstats.exp $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}
if ($sort=='credits'){ 

// $result = mysql_query("SELECT * FROM userstats ORDER BY credits"); 
$result = mysql_query("SELECT * FROM userstats LEFT JOIN users ON users.id = userstats.id ORDER BY userstats.credits $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
}

if ($delete && isset($_GET['id']))
{
    mysql_query('DELETE FROM users WHERE id = ' . (int)$_GET['id']);
mysql_query('DELETE FROM userstats WHERE id = ' . (int)$_GET['id']);
echo "<SCRIPT language='JavaScript'><!--
  window.location='users.php';//-->
</SCRIPT>";
}  

if ($ban=="true" && isset($_GET['id']))
{
    mysql_query('UPDATE `users` SET `active`="no" WHERE id = ' . (int)$_GET['id']);
echo "<SCRIPT language='JavaScript'><!--
  window.location='users.php';//-->
</SCRIPT>";
}  
if ($ban=="false" && isset($_GET['id']))
{
    mysql_query('UPDATE `users` SET `active`="yes" WHERE id = ' . (int)$_GET['id']);
echo "<SCRIPT language='JavaScript'><!--
  window.location='users.php';//-->
</SCRIPT>";
}  

// head
echo "
<html>
<head>
<title>Users</title>
<style>
a:link{
text-decoration: none;
color: #519904;
}
a:visited{
text-decoration: none;
color: #519904;
}
a:hover{
text-decoration: none;
color: #4296ce;
}
#headcont{
width: 900px;
height: 20px;
margin-left: auto;
margin-right: auto;
}
#free{
width: 225px;
text-align: center;
float: left;
background-color: #cccccc;
}
#joined{
width: 225px;
text-align: center;
float: left;
background-color: #800000;
color: #ffffff;
}
#upgraded{
width: 225px;
text-align: center;
float: left;
background-color: #72A4D2;
}
#refer{
width: 225px;
text-align: center;
float: left;
background-color: #778899;
color: #ffffff;
}
</style>
</head>
<body>
";

echo "<div style='font-size: 28px; text-align: center;'>Users</div>
<div id='headcont'>
<div id='free'>Free Members: ".$freecount."</div>
<div id='upgraded'>Upgraded Members: ".$typecount."</div>
<div id='refer'>Not Referred: ".$referresult."</div>
<div id='joined'>Joined Today: ".$joindateresult."</div>
</div><br />
<table border='1' align='center'>
<tr>
<th bgcolor='#cccccc'><a href='users.php?sort=id'>ID</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=username'>Username</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=email'>Email</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=type'>Type</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=referrer'>Referrer</a></th>

<!-- Level, Exp, and Credits are in the table called userstats -->

<th bgcolor='#cccccc'><a href='users.php?sort=level'>Level</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=exp'>Exp</a></th>
<th bgcolor='#cccccc'><a href='users.php?sort=credits'>Credits</a></th>
<th bgcolor='#cccccc'><a href='users.php'>Delete</a></th>
<th bgcolor='#cccccc'><a href='users.php'>Ban</a></th>
</tr><form action='users.php' method='POST'>";
echo "<script type='text/javascript'>
function show_ban()
{
var r=confirm('Ban?');
if (r==true)
{
// Ban
return true;
}
else
{
// Don't ban
return false;
}
}
";

echo "
function show_unban()
{
var r=confirm('Unban?');
if (r==true)
{
// Unban
return true;
}
else
{
// Don't Unban
return false;
}
}
";

echo "
function show_delete()
{
var r=confirm('Delete?');
if (r==true)
{
// Delete
return true;
}
else
{
// Don't delete
return false;
}
}
</script>";


while($row = mysql_fetch_array($result))
  {
  $active = $row['active'];
  $color = "#ffffff";
  $banned = "Ban";
  if ($active=='no'){
  $color = "#f43636";
  $banned = "Unban";
  $active = "false";
  $alert = "show_unban";
  }
  else{
  $active = "true";
  $alert = "show_ban";
  }
  if ($row['member'] == 1){
  $typecolor = "#72A4D2";
  }
  if ($row['member'] == 0){
  $typecolor = "#eeeeee";
  }
  if ($row['member'] == 9){
  $typecolor = "#00cc00";
  }
  
  
  echo "<tr>";
  echo "<td align='center' width='40' bgcolor='$color'>" .$row['id']. "</td>";
  echo "<td align='center' width='130'><input type='text' name='username' value='" .$row['username']. "'></td>";
  echo "<td align='center' width='230'><input type='text' name='email' value='" .$row['email']. "' size='35'></td>";
  echo "<td align='center' width='10'><input type='text' name='member' value='" .$row['member']. "' size='2' style='background-color: $typecolor'></td>";
  echo "<td align='center' width='130'><input type='text' name='referrer' value='" .$row['referrer']. "'></td>";
  echo "<td align='center' width='10'><input type='text' name='level' value='" .$row['level']. "' size='2'></td>";
  echo "<td align='center' width='10'><input type='text' name='exp' value='" .$row['exp']. "' size='4'></td>";
  echo "<td align='center' width='10'><input type='text' name='credits' value='" .$row['credits']. "' size='20'></td>";
  echo "<td align='center' width='10'><a href='users.php?delete=true&id=" .$row['id']. "' onclick='return show_delete()'>Delete</a></td>";

  echo "<td align='center' width='10'><a href='users.php?ban=$active&id=" .$row['id']. "' onclick='return $alert()'>$banned</a></td>";
  echo "</tr>";
  }

echo "</table><br /><center><input type='submit' name='submit' value='Submit Changes'><input type='reset' name='reset' value='Reset'></form></center>";



// Footer
echo "
</body>
</html>
";

// Change User's Information

if (isset($submit)){

// UPDATE USERS INFORMATION FOR ONLY THE ROWS THAT HAVE BEEN MODIFIED
for($i=0;$i<count($_POST);$i++){

     // $update_query = "UPDATE users SET username='".mysql_real_escape_string($_POST[$i]['username'])."' WHERE id=".(int)$_POST[$i]['user_id'];
     // mysql_query($update_query);
}

}
?>

 

Well actually your for loop is correct however because you did not use my code your code is not correct. The input fields that you have look like "username" or "password" mine looked like "username[]" or "password[]". The reason for that is that it then sends that data as a multi-dimensional array with all of the users info instead of just one user like yours. With a multi dimensional array your data would look like:

 

Array ( 
[0]=>Array( [username] => cookiemonster  => [email][email protected] [member] => 0 [referrer] => [level] => 1 [exp] => 0 [credits] => 50.000 [submit] => Submit Changes ) 
[1]=>Array(//another user here)
)

 

That is what you would loop through to update each user. Also you didnt add the hidden field for the id so you would not be able to update the database correctly. I took the time to update and fix your code to make it more readable and made alot of fixes to the code. You may want to try using mine and troubleshooting any errors you get at that point. If you dont update your code, then I will not be able to help you and unwilling.

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.