Help With HTML Select and MySQL Delete

Discussion in 'PHP' started by rspinuz, Nov 1, 2010.

?

Do you agree with the replys?

  1. Yes

    0 vote(s)
    0.0%
  2. No

    0 vote(s)
    0.0%
  3. Maby

    0 vote(s)
    0.0%
  4. There is a better way to do this.

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. #1
    The code is posted here: codepad.org/Fck5s2zz
    I have also attached the code as (delete_user.php)
    The same code is also posted below.

    [text]
    What I am trying to do is create a function that will allow me to delete a user from my mysql database.

    I have an HTML select that outputs the username(s) using the function get_admin_username()
    (The function is posted below)

    There is also a HTML form I am using to make this happen.
    (Also posted below)

    In other words I would select the user I would like to delete, and click submit which will then delete the user from the database.

    The problem I am having is I do not know how to pass values from the HTML select, or the form, or even use the submit button value.

    I am not sure If I need to create a function to achieve this.
    Basically I don't know what to do this at all for that matter.

    I thought I might have gotten close but I thought wrong, and failed.
    I was told (in the php irc) that my method was way off and wrong but was given no help after that.

    I do have an example to show you of how I tried to accomplish this, but being told that I was wrong I did not feel that it was even worth posting it.

    I am lost and have been at this for two days now. I am a noobe but I do understand allot (I think).

    Someone, anyone, please help.

    Thank You,

    Ryan
    [/text]

    Code:

    
    <?php
    
       // Session's Functions
       require_once("../includes/sessions/session.php");
    
       // Establish A Connection To The Database
       require_once("../includes/connection/connection.php");
    
       // User Defined Admin Functions
       require_once("includes/functions/admin_functions.php");
    
       // New User Functions
       //require_once("includes/users/delete_user.php");
    
       // Confirms If The User Is Logged In
       confirm_logged_in();
    
       // Document Header
       include("includes/templates/default/header.php");
    
    ?>
    
    
    <?php
       // Gets The Admin Username
       function get_admin_username()
          {
             global $connection;
             $query = "SELECT * FROM administration_users ";
             $query .= "ORDER BY username";
             $admin_user_set = mysql_query($query, $connection);
             confirm_query($admin_user_set);
    
             while ($admin_users = mysql_fetch_array($admin_user_set))
                {
                   echo "<option value=\"username" . "\">" . $admin_users['username'] ."\n  ";
                }
          }
    ?>
    
    
          <table id="structure">
             <tr>
                <td id="navigation">
                   <a href="../staff.php">Return To Staff Menu</a>
                   <a href="admin_content.php">Return To Admin Menu</a>
    <br />
    <br />
                   <ul class="menu">
                      <li class="pages"><a id="page_menu" href="new_user.php">Add New User</a></li>
                      <li class="pages"><a href="edit_user.php">Edit User</a></li>
                      <li class="pages"><a href="list_users.php">List Users</a></li>
                      <li class="pages"><a href="delete_user.php">Delete User</a></li>
                   </ul>
                </td>
    
                <td id="page">
                   <h2>Remove User</h2>
    
    <br />
    
                   <form action="delete_user.php" name="delete_user" method="post">
                      <table>
                         <tr>
                            <th class="field_name field_padding_user_name">User Name: &nbsp;</th>
    
                            <td>
                               <select id="select_username" name="username_select">
                                  <?php echo get_admin_username();?>
                               </select>
                            </td>
                         </tr>
    
                         <tr>
                            <td class="delete_user_submit" colspan="2"><input type="submit" name="submit" value="Delete User"
                                 onclick="return confirm('Are You Sure You Want To Delete This User?');"/></td>
                         </tr>
                      </table>
                   </form>
                </td>
             </tr>
          </table>
    
    
    <?php
    
       // Document Footer
       include("includes/templates/default/footer.php");
    
    ?>
    
    PHP:
    View attachment delete_user.php
     
    rspinuz, Nov 1, 2010 IP
  2. Javed iqbal

    Javed iqbal Well-Known Member

    Messages:
    445
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #2
    if you want work from me contact me
     
    Javed iqbal, Nov 2, 2010 IP
  3. rspinuz

    rspinuz Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No I want your help.
     
    rspinuz, Nov 2, 2010 IP
  4. Blue Link Media

    Blue Link Media Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if (isset($_POST['username_select']) && strlen($_POST['username_select']) != 0)
    {
    	$username = mysql_real_escape_string($_POST['username']);
    
    	mysql_query("DELETE FROM administration_users WHERE username='$username'") or die(mysql_error());
    }
    PHP:
    Attitude.
     
    Blue Link Media, Nov 2, 2010 IP
  5. rspinuz

    rspinuz Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    To: Blue Link Media,

    I did not mean for it to sound like an attitude. I was more or less trying to sound like a smart ...

    Thank You for your help. I will give this a try.
     
    Last edited: Nov 2, 2010
    rspinuz, Nov 2, 2010 IP
  6. rspinuz

    rspinuz Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    OK. I have gotten the result I wanted. (To delete the selected user).
    However is this a proper way to write the code?



    
    <?php
    
    	// Gets The Admin Username
    	function get_admin_username() 
    		{
    			global $connection;
    			$query = "SELECT * FROM administration_users ";
    			$query .= "ORDER BY username";
    			$admin_user_set = mysql_query($query, $connection);
    			confirm_query($admin_user_set);
    
    			while ($admin_users = mysql_fetch_array($admin_user_set)) 
    				{
    					echo "<option value=\"username" . "\">" . $admin_users['username'] ."\n  ";
    				} 
    		}
    
    
    
    	// Deletes The Selected User
    	if (isset($_POST['submit']) && 'username')
    		{
    			$query= mysql_query("DELETE FROM administration_users WHERE username={$_POST['username_select']} LIMIT 1") or die(mysql_error());
    		}
    
    ?>
    
    PHP:
     
    rspinuz, Nov 2, 2010 IP
  7. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #7
    there's a few bits you can do:
    
    <?php
    
        // Gets The Admin Username
        function get_admin_username() 
            {
                //no need for global or over 2 lines, removed * as only use username
                $query = "SELECT username FROM administration_users ORDER BY username";
                $admin_user_set = mysql_query($query);
                //removed confirm query, seemed to do nothing
    
                while ($admin_users = mysql_fetch_array($admin_user_set)) 
                    {
                     //removed unneeded opening and closing and added in close of option
                        echo "<option value=\"username\">$admin_users[username]</option>\n";
                    } 
            }
    
    
    
        // Deletes The Selected User
        //'username' did nothing
        if (isset($_POST['submit']))
            {
                $query= mysql_query("DELETE FROM administration_users WHERE username='{$_POST['username_select']}' LIMIT 1") or die(mysql_error());
            }
    
    ?>
    
    PHP:
     
    themullet, Nov 2, 2010 IP