PHP Diary | scriptschool.com | PHP Scripts | TD Scripts.com
Online Offline Manager - Show the online/offline status of a group of users on a webpage


[back]
go back 07/20/00 "Admin user interface: adding and deleting users" go forward[next]

Easy come, easy go in SQL

Our next step in creating a page by page validation is to create the actual form the admin sees. The simplest way for the delete functionality is to make an alphabetically sorted list of usernames and then a button to delete. In my last diary entry I explored creating a dynamic drop down list, now we need to take that one slight step further and use the SQL statement ORDER BY which will put the data returned in alphabetical ascending or descending order. The default is ASC which is ascending order (abc... 123...) so we do not need to put the ASC in the SELECT statement. We only need to slightly embellish upon the code from yesterday to change this. But first, let's take a look at what the actual form might look like if we had no users yet:

Delete Users DEMO ONLY, form does not work

The code for creating this form with a dynamic user alphabetically sorted drop down list looks like this:

<form method="POST" action="admin.php3"><div align="center"><center><p>Delete Users
<input type="hidden" name="react" value="delete_user">
<
select name="user" size="1">
<script language="php">
   $query = "SELECT user FROM login ORDER BY user";
   $result = mysql_query($query, $mysql_link);
     if(mysql_num_rows($result)) {
       // we have at least one user, so show all users as options in select form
       while($row = mysql_fetch_row($result))
       {
          print("<option value=\"$row[0]\">$row[0]</option>");
       }
     } else {
       print("<option value=\"\">No users created yet</option>");
     }
</script>

<
/select><input type="submit" value="Submit"></center></div>
<
/form>

Note the line bolded in green. This is our hidden input field which will flag our admin script and tell it that we need to goto the branch of code for deleting users. This way we can self-contain our admin script in one script instead of having to call a separate script. The option values will be script-generated, using the php function mysql_query and the correctly prepared SQL query. If you need more help on how the dynamic dropdown select list is assembled see the last diary entry.

Next, to add users we need a form with two text boxes. One labeled username and the other password. It might look something like this:

ADD A New User
User:
Pass:

The form above HTML code looks like this:

<form method="POST" action="072000.php3">
<input type="hidden" name="react" value="add_user">
<
div align="center"><center><p>ADD A New User<br>
User: <
input type="text" name="user" size="20"><br>
Pass: <
input type="text" name="pass" size="20"><br>
<
input type="submit" value="Submit"></p></center></div>
</form>

Again, note the line bolded in green. This is our hidden input field which will flag our admin script and tell it that we need to goto the branch of code for adding users. Now that the HTML area is done we need to build the PHP code that happens before we'd ever see the forms above. We'll have 3 branches in the code, the first branch will delete users, the second will add and if we aren't adding or deleting users then we'll display the two forms above.

Our PHP Admin code

Now that we have assembled the form to call the admin.php3 script we can work on the code BEFORE the admin would ever see the forms above. When building a standalone script with branches it helps to imagine the project visually with the everything at the top being what will be most priority and everything at the bottom being a lower priority. Functions would likely be stored at the top or the beginning of your scripts, as well as calls to require other library files or other important files you need for the execution of the script. In our admin script the first thing we need is the branch definition to determine what we are going to do when the script is called.

<HTML>
<script language="php">
if($react == "delete_user" {
   // delete user here
}
elseif ($react == "add_user") {
   // add user here
}
else {
   print("<center>Administration Area - Choose your option</center>");
}
</script>

As you can see above the code we used the if, elseif and else to determine where to go in the code. We check the $react variable culled from the hidden input form fields which would be coming from within the script. If the value doesn't equal "delete_user" or "add_user" we print the administration area - choose your option to the browser and go on to the rest of the code aforementioned. Next let's create the code to delete the user. This is done with the SQL statement DELETE. I will warn you now that this is a very powerful statement so be careful with it. You can erase an entire table's contents with one SQL statement, so always make sure you are including a WHERE clause as described below.

DELETE from login WHERE user='$user'

Without the WHERE user='$user' you would be deleting the entire login table! Keep this in mind as it is a very easy mistake to make when coding and if you make it, you'll lose all your table data, and there isn't a way to recover it. Now we simply need to embed the SQL statement into a $query variable and use the php function mysql_query to complete the action. Here's the code to do this:

if($user) {
$query = "DELETE from login WHERE user='$user' ";
   $result = mysql_query($query, $mysql_link);
     if(mysql_num_rows($result)) {
       print("<strong>$user</strong> successfully deleted<p>");
     }
} else {
  print("<strong>no users are available to delete yet, sorry.</strong><p>");
}

The first line if($user) checks to make sure that $user variable is not empty which would indicate that there are no users to actually delete. Note the code above in the dynamic drop down list that if we had no users the message "No users created yet" is displayed and the option value="" so this is the trigger in the script to tie this up in case the admin tries to delete an invalid user. This is one type of trap, though of little use, it does make sure that you are responding to a possible action. The message will of course not be there once a user is created. The next code executes the query and if there was a match then it displays the message that the $user was successfully deleted.

Now let's look at the code to add the users. We discussed how this could be done 2 lessons ago so we only need to insert the code we already discussed inside the add_users elseif branch, adding a similar qualifier to make certain both fields contain something (are true) like this:

if(($user) and ($pass)) {
$query = "INSERT into login VALUES ( ";

$query .= "0, SYSDATE(), '$username', '$password' )";
mysql_query($query, $mysql_link);
} else {
print("<strong>either your user or password field was left blank</strong><p>");
}

We have now created a fully workable admin script to add and remove users from the login table we created using mySQL. To see the entire admin.php3 script you can click on the example below.

Example #27: View a complete admin script for adding and deleting users using mySQL

Please vote on the usefulness of this diary entry so other people will know if it is worth their time to read :)

How useful was this diary entry? Avg Surfer Rating: 3.79 (333)

[back]go back 07/20/00 "Admin user interface: creating dynamic dropdown lists in mySQL" go forward[next]

PHP Diary | scriptschool.com | PHP Scripts | TD Scripts.com

Copyright 2000 php-scripts.com Last Modified 07/27/00 11:00