Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2004
    Location
    Salem, Massachusetts. USA
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Form editing and deletion with MySQL

    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.

    PHP Code:
    $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">
    &nbsp;&nbsp;&nbsp;
    <input type="reset" value="Reset" onClick="return confirm(\'Are you sure?\');" class="submit">
    </p>'
    );
    }
    ?>
    </form>

  • #2
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,062
    Thanks
    4
    Thanked 8 Times in 8 Posts
    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.

  • #3
    New Coder
    Join Date
    Feb 2004
    Location
    Salem, Massachusetts. USA
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Last edited by punkpuke; 04-08-2004 at 08:35 PM.

  • #4
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,062
    Thanks
    4
    Thanked 8 Times in 8 Posts
    I will be on the road for a few days but will help you with the script the beginning of the week.

  • #5
    New Coder
    Join Date
    Feb 2004
    Location
    Salem, Massachusetts. USA
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, I appreciate.

  • #6
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,062
    Thanks
    4
    Thanked 8 Times in 8 Posts
    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>');

    becomes

    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.

  • #7
    New Coder
    Join Date
    Feb 2004
    Location
    Salem, Massachusetts. USA
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •