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!

Recommended Answers

All 2 Replies

First:
You need to put some debug code in (e.g. an echo) to see what is coming back from your confirm window. If it is what you expected, then obviously your code isn't handling the cancel response correctly.

Second:
If you are showing all the details for a range of users on a single page, then you will need to create the form using a loop and the names of the fields will have the format:
field1$i
where $i is your loop counter.
When the code that receives these fields processes them, it will have to treat them as variable variables (to avoid having to hard code every possibility). Thus, you will need to loop through the possible range of fields processing one row (one $i value) at a time. In each case, you will need to get the value of the fields into a standard set of field names and then do your processing:

$var = "field1".$i;                 
$field1 = $$var;

Third:
You can easily create a drop-down menu in PHP by using <Select> <option...> HTML statements as part of your form (drop-down menus). Unless you have an initial panel on entry to the Admin facility, this will require a refresh of the page in order to take effect. Obviously, in your code to display the user details in the output, you will need to have a loop that is controlled by whatever value is chosen (with a default). It would probably work better using Javascript (search).

EDIT: The first problem is solved.

For the second problem (submitting user's new information), why are you using javascript? Can't I do the same thing with PHP. Also, could you please elaborate on how to create a script to do this that works with my current code? I've never done something like this before.

And the last problem I don't understand how to set it up, but it really isn't my priority compared to "submit".

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.