View Full Version : PHP Form editing and deletion with MySQL

04-07-2004, 09: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");

$delete = array();

// Delete User/s
for($i = 0; $i <= count($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");
die("No User Specified In Deletion");
<title>Forgotten Wraith's</title>
<link rel="stylesheet" href="../css/main.css" type="text/css" />
<script language="javascript" src="../js/main.js"></script>

<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>
$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;
while ($row = mysql_fetch_assoc($all_logins_result))
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. ']">');


if($lv == $row['level']){
echo('<option value="' . $lv . '" selected>' . $lv);
echo('<option value="' . $lv .'">' . $lv);
<td bgcolor="#e1e1e1"><input name="delete[' .$num. ']" value="' .$row['id']. '" type="checkbox"></td></tr>');
echo '<tr><td bgcolor="#e1e1e1">No records found.</td></tr>';
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">

04-08-2004, 01:13 AM
First off why would the id # change in your database??? it would never change without you doing an update statement on the id# field itself. If it is a unique identifier then you really should not allow it to ever be changed. which leads me to the next question. What is the datatype of the id column?

now lets assume that it is an integar field, is set to auto increment so that each new record is different than the previous, and is set as the Unique or primary. by simply deleting a record you do not change the id number of any other records. you just delete that record and the number corresponding to it will not be added ever again.

my next question is what are you trying to accomplish using a for loop in the delete? Unless you want to delete a whole group of users at one time, there is no need to do any type of loop in a delete.

Let me know what you are trying to do and I will help you get through this.

04-08-2004, 09:10 PM
It is an integar and it is auto_increment. I did not know what primary or unique was until now. Somebody told me to make it Primary when I created it. That is what it is right now. So, I guess I'm making this harder then it seems. But, what should I name the check boxes for each user? And How would I be able to get whatever they are named?

04-10-2004, 05:26 PM
I will be on the road for a few days but will help you with the script the beginning of the week.

04-10-2004, 08:00 PM
Thanks, I appreciate.

04-13-2004, 05:18 AM
Basically what you have should work with one minor change.

echo('</select></td><td bgcolor="#e1e1e1"><input name="delete[' .$num. ']" value="' .$row['id']. '" type="checkbox"></td></tr>');


echo('</select></td><td bgcolor="#e1e1e1"><input name="delete" value="' .$row['id']. '" type="checkbox"></td></tr>');

There is no need to change the name of the delete box. (name="delete[' .$num. ']" will change the name of each box) After all, you want to build an array on this to allow for multiple deletions correct? If you change the name and select multiple people for deletion, then you will have multiple arrays,and each array will have 1 item in it. This small change will build a single array with a value for each of the selected users.

04-13-2004, 07:12 AM
Yes!, I figured this out in another forum that I suscribe to. They told me the same thing just about. I still thank you Very much for your help.