I am stuck on a problem, and can't seem to figure out why this isn't working, when I am pretty sure it should be...
I am trying to get my code to delete users from the dropdown list after the admin hits the delete button within the page. The form's action is to delete_user.php
Here is the add_delete_users.php page:
<?php
require('config.php');
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAMETWO);
if (mysqli_connect_errno()) {
die("Connection Failed");
}
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
} // if
?>
<!DOCTYPE html>
<html>
<head>
<title>Manage Users</title>
<link type="text/css" rel="stylesheet" href="/css/layout.css" />
<link type="text/css" rel="stylesheet" href="/css/admin.css" />
<!--[if lte IE 7]><link rel="stylesheet" href="/css/adminie7.css" type="text/css" media="screen" /><![endif]-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="/js/contentArea.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#content .row:odd').addClass('odd');
$('#content .row:even').addClass('even');
});
</script>
</head>
<body id="index">
<div class="container-12">
<h1>Manage Users</h1>
<div id="content">
<ul id="statuses">
<li <?php if ($status === "") {echo "class=\"current_page\"";}?>><a href="index.php?&status=" id="nav-new">New</a></li>
<li <?php if ($status === "assigned") {echo "class=\"current_page\"";}?>><a href="index.php?&status=assigned" id="nav-assigned">Assigned</a></li>
<li <?php if ($status === "completed") {echo "class=\"current_page\"";}?>><a href="index.php?&status=completed" id="nav-completed">Completed</a></li>
<li><a href="bought.php">Bought</a></li>
<li class="current_page"><a href="add_delete_users.php">Users</a></li>
</ul>
<div class="clear"></div>
<?php
$sql = "SELECT * FROM users";
$result = mysqli_query($mysqli, $sql) or trigger_error("SQL", E_USER_ERROR);
while ($row = mysqli_fetch_array($result)) {
$name = $row['name'];
$options.="<OPTION VALUE=". $name .">". $name ."</option>";
}
echo"<lable><strong>Delete User</strong></lable><form class=\"delete-form\" method=\"post\" action=\"delete_user.php\"><select name=\"user_name\" id=\"user_name\">\n" . $options . "</select>
<input type=\"submit\" value=\"- Delete User\" /></form><br /><br />";
echo"<lable><strong>Add User</strong></lable><form class=\"add-form\" method=\"post\" action=\"add_user.php\">
<input type=\"hidden\" name=\"user_id\" value=\"\" /><br />
<lable for=\"user_name\">User Name: </lable><input type=\"text\" name=\"user_name\" value=\"\" /><br />
<lable for=\"email\">Email: </lable><input type=\"text\" name=\"email\" value=\"\" /><br />
<input type=\"hidden\" name=\"password\" value=\"2f0727a8fd0c695e52bfa79a97b6c08ab418c1db\" />
<lable for=\"name\">Name: </lable><input type=\"text\" name=\"name\" value=\"\" /><br />
<input type=\"hidden\" name=\"privileges\" value=\"admin\" /><br />
<input type=\"submit\" value=\"+ Add User\" /></form>";
mysqli_close($mysqli);
?>
</div>
</div>
</div>
</body>
</html>`
And here is the delete_user.php script (separate page completely)!
<?php
require('config.php');
$names = ($_POST['user_name']);
// Connect to Database to store information
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAMETWO);
if (mysqli_connect_errno()) {
printf("Connect Failed: %s\n", mysqli_connect_error());
} else {
$sql = "DELETE FROM users WHERE user_name = '$names' ";
$res = mysqli_query($mysqli, $sql);
echo "$names has been deleted!<br />";
echo "<a href=\"add_delete_users.php\">Go back to Manage more users</a><br />
<a href=\"index.php?&status=\">Go back to the main page</a><br />";
mysqli_close($mysqli);
}
// End Database interactions
?>
Now I have tried a couple of different iterations of SQL the delete, like $sql = "DELETE FROM users WHERE name = '$names' ";
and then the current one, and they have both worked a couple of times, but then suddenly stop working, without me even changing any of the code!
Pretty weird if you ask me... Anyways, I am sure it is something I have overlooked due to my eyes not being fresh to the code...
Any help is appreciated!
Thanks!