Page 1 of 1

Help With HTML Select and MySQL Delete

Posted: Tue Nov 02, 2010 12:30 am
by rspinuz
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

Code: Select all

<?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");

?>

Re: Help With HTML Select and MySQL Delete

Posted: Tue Nov 02, 2010 3:50 am
by klevis miho
Find a way to get the id of the user that you want to delete. Then change this line:
<li class="pages"><a href="delete_user.php">Delete User</a></li>
to:
<li class="pages"><a href="delete_user.php?user_id=<?php echo $user_id; ?>">Delete User</a></li>

Then, in the delete_user.php write get the user_id variable like this:
$user_id = $_GET['user_id'];

Then do a delete query:
DELETE FROM administration_users WHERE id = $user_id