punkpuke
04-07-2004, 08:31 PM
Hi, this is my second or third attempt at making a form that can edit or delete. This script makes a list of users for site login names on my website. I want it to be an "admin page". That lists users with 2 forms next to each ones name (One for Deletion and one for the Level of the user's access). I have the usernames in my, MySQL database. My problem is once the form is entered, how to then get the data and process it, because the forms are never the same number and stuff (considering the usernames in the list get deleted and added).
The most important thing I want to be able to do is Delete the user. Each user gets a Checkbox input next to their name. When checked and entered I want it to delete that user. But, I tried and I can't do this. I made a newbish attempt.
The second thing I did was make a drop down menu that automatically puts the Level of the user (0-5) next to the name. By default each user has a level of 0. If I wanted to change it I assumed I would need EDIT or something. I didn't attempt this, because I just wanted to learn the basic way of getting stuff from random forms. But, if somebody could show me how it would be really nice. Thanks for you time.
$link = mysql_connect ("localhost","username","******") or die("Error: No connection to MySQL server");
$database = mysql_select_db("forgottenwraiths_com_main") or die("Error: No Database Connection");
if ($REQUEST_METHOD == 'POST')
{
$delete = array();
extract($_POST);
// Delete User/s
for($i = 0; $i <= count($delete); $i++)
{
if(isset($delete[$i]))
{
$delete_user_sql = "DELETE FROM users WHERE id='$delete[$i]'";
mysql_query($delete_user_sql, $link) or die("There Was An Error Deleting Users");
}
else
{
die("No User Specified In Deletion");
}
}
}
?>
<html>
<head>
<title>Forgotten Wraith's</title>
<link rel="stylesheet" href="../css/main.css" type="text/css" />
<script language="javascript" src="../js/main.js"></script>
</head>
<body marginwidth="0" marginheight="0" style="margin:0;">
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr align="center" valign="top">
<td align="left" valign="top">
<h2 align="center">Users & Access</h2>
<form action="usersaccess.php" method=post name="user_edit" style="margin:0;">
<table cellpadding="1" cellspacing="5" border="1" width="100%" class="rankblock">
<th bgcolor="#e1e1e1">Member</th>
<th bgcolor="#e1e1e1">Username</th>
<th bgcolor="#e1e1e1">Password</th>
<th bgcolor="#e1e1e1">Level</th>
<th bgcolor="#e1e1e1">Delete</th>
<?php
$all_logins_sql = "SELECT id, username, password, name, level FROM users WHERE id >= 0";
$all_logins_result = mysql_query($all_logins_sql, $link) or die("There Was An Error Loading The Users List.");
if (mysql_num_rows($all_logins_result) >= 1){
$submit_button = TRUE;
$num=0;
while ($row = mysql_fetch_assoc($all_logins_result))
{
$num++;
echo('<tr><td bgcolor="#e1e1e1">' . $row['name'] . '</td>
<td bgcolor="#e1e1e1">' . $row['username'] . '</td>
<td bgcolor="#e1e1e1">' . $row['password'] . '</td>
<td bgcolor="#e1e1e1"><select name="level[' .$num. ']">');
for($lv=0;$lv<6;$lv++){
if($lv == $row['level']){
echo('<option value="' . $lv . '" selected>' . $lv);
}
else{
echo('<option value="' . $lv .'">' . $lv);
}
}
echo('</select></td>
<td bgcolor="#e1e1e1"><input name="delete[' .$num. ']" value="' .$row['id']. '" type="checkbox"></td></tr>');
}
}
else
{
echo '<tr><td bgcolor="#e1e1e1">No records found.</td></tr>';
}
?>
</table>
<?php
if($submit_button == TRUE)
{
echo ('<p align="center">
<input type="submit" value="Edit Users" class="submit">
<input type="reset" value="Reset" onClick="return confirm(\'Are you sure?\');" class="submit">
</p>');
}
?>
</form>
The most important thing I want to be able to do is Delete the user. Each user gets a Checkbox input next to their name. When checked and entered I want it to delete that user. But, I tried and I can't do this. I made a newbish attempt.
The second thing I did was make a drop down menu that automatically puts the Level of the user (0-5) next to the name. By default each user has a level of 0. If I wanted to change it I assumed I would need EDIT or something. I didn't attempt this, because I just wanted to learn the basic way of getting stuff from random forms. But, if somebody could show me how it would be really nice. Thanks for you time.
$link = mysql_connect ("localhost","username","******") or die("Error: No connection to MySQL server");
$database = mysql_select_db("forgottenwraiths_com_main") or die("Error: No Database Connection");
if ($REQUEST_METHOD == 'POST')
{
$delete = array();
extract($_POST);
// Delete User/s
for($i = 0; $i <= count($delete); $i++)
{
if(isset($delete[$i]))
{
$delete_user_sql = "DELETE FROM users WHERE id='$delete[$i]'";
mysql_query($delete_user_sql, $link) or die("There Was An Error Deleting Users");
}
else
{
die("No User Specified In Deletion");
}
}
}
?>
<html>
<head>
<title>Forgotten Wraith's</title>
<link rel="stylesheet" href="../css/main.css" type="text/css" />
<script language="javascript" src="../js/main.js"></script>
</head>
<body marginwidth="0" marginheight="0" style="margin:0;">
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr align="center" valign="top">
<td align="left" valign="top">
<h2 align="center">Users & Access</h2>
<form action="usersaccess.php" method=post name="user_edit" style="margin:0;">
<table cellpadding="1" cellspacing="5" border="1" width="100%" class="rankblock">
<th bgcolor="#e1e1e1">Member</th>
<th bgcolor="#e1e1e1">Username</th>
<th bgcolor="#e1e1e1">Password</th>
<th bgcolor="#e1e1e1">Level</th>
<th bgcolor="#e1e1e1">Delete</th>
<?php
$all_logins_sql = "SELECT id, username, password, name, level FROM users WHERE id >= 0";
$all_logins_result = mysql_query($all_logins_sql, $link) or die("There Was An Error Loading The Users List.");
if (mysql_num_rows($all_logins_result) >= 1){
$submit_button = TRUE;
$num=0;
while ($row = mysql_fetch_assoc($all_logins_result))
{
$num++;
echo('<tr><td bgcolor="#e1e1e1">' . $row['name'] . '</td>
<td bgcolor="#e1e1e1">' . $row['username'] . '</td>
<td bgcolor="#e1e1e1">' . $row['password'] . '</td>
<td bgcolor="#e1e1e1"><select name="level[' .$num. ']">');
for($lv=0;$lv<6;$lv++){
if($lv == $row['level']){
echo('<option value="' . $lv . '" selected>' . $lv);
}
else{
echo('<option value="' . $lv .'">' . $lv);
}
}
echo('</select></td>
<td bgcolor="#e1e1e1"><input name="delete[' .$num. ']" value="' .$row['id']. '" type="checkbox"></td></tr>');
}
}
else
{
echo '<tr><td bgcolor="#e1e1e1">No records found.</td></tr>';
}
?>
</table>
<?php
if($submit_button == TRUE)
{
echo ('<p align="center">
<input type="submit" value="Edit Users" class="submit">
<input type="reset" value="Reset" onClick="return confirm(\'Are you sure?\');" class="submit">
</p>');
}
?>
</form>