Craptacular Posted May 17, 2012 Share Posted May 17, 2012 I know this is something I am just overlooking, but please help. I can't seem to get the selected item in the dropdownlist that is being populated from a database to be deleted. Here is the add_delete_users.php <?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 that is being called after the user presses the delete button. <?php require('config.php'); $user_name = ($_POST['options']); // 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 name = $user_name"; $res = mysqli_query($mysqli, $sql); echo "$user_name 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 ?> Go easy on me. At this point my brain is a bit fried... I just need someone else's eyes on it at this point. Thanks in advance! Quote Link to comment Share on other sites More sharing options...
Barand Posted May 17, 2012 Share Posted May 17, 2012 Your select has name "user_name", not "options", therefor the username will be in $_POST['user_name'] Quote Link to comment Share on other sites More sharing options...
Craptacular Posted May 17, 2012 Author Share Posted May 17, 2012 Okay. I did that, and it is showing the echo but isn't actually deleting it from my db... Quote Link to comment Share on other sites More sharing options...
Barand Posted May 17, 2012 Share Posted May 17, 2012 Should be DELETE FROM users WHERE name = '$user_name' Note the quotes around $user_name Quote Link to comment Share on other sites More sharing options...
Craptacular Posted May 17, 2012 Author Share Posted May 17, 2012 Nope... Still not working. :'( Quote Link to comment Share on other sites More sharing options...
Barand Posted May 17, 2012 Share Posted May 17, 2012 What does mysql_error($mysqli) return? Quote Link to comment Share on other sites More sharing options...
Craptacular Posted May 18, 2012 Author Share Posted May 18, 2012 What does mysql_error($mysqli) return? It doesn't return anything. BUT because I was fried yesterday, after you told me to do the single quotes around the var, I didn't notice it was actually working!!! I noticed thins morning when I go to work and tested out the mysqli_error($mysqli). SO, thank you VERY much! I knew it was something simple I just wasn't able to see.... As usual! Thanks again, Barand! Quote Link to comment Share on other sites More sharing options...
Craptacular Posted May 18, 2012 Author Share Posted May 18, 2012 I spoke too soon... It worked and then without changing any of the code, it stopped working. Very curious, no? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted May 18, 2012 Share Posted May 18, 2012 Please post your updated code. Quote Link to comment Share on other sites More sharing options...
Craptacular Posted May 18, 2012 Author Share Posted May 18, 2012 delete_user.php is the only one I updated: <?php require('config.php'); // Connect to Database to store information $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAMETWO); $names = mysqli_real_escape_string($mysqli, $_POST['user_name']); 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 ?> But here is the form page as well... <?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> Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted May 18, 2012 Share Posted May 18, 2012 $sql = "DELETE FROM users WHERE user_name = '$names' "; $res = mysqli_query($mysqli, $sql); echo "<pre>$sql</pre>" . mysqli_error(); What does that give? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted May 18, 2012 Share Posted May 18, 2012 Edit: misread code. Tip: only pull from a table what you will be using in your script. For example: $sql = "SELECT * FROM users"; is overkill when you only need to access `name`. Revise, and subsequently keep in mind for future queries, to the following: $sql = "SELECT `name` FROM users"; Quote Link to comment Share on other sites More sharing options...
Craptacular Posted May 18, 2012 Author Share Posted May 18, 2012 $sql = "DELETE FROM users WHERE user_name = '$names' "; $res = mysqli_query($mysqli, $sql); echo "<pre>$sql</pre>" . mysqli_error(); What does that give? It says: DELETE FROM users WHERE user_name = 'Michelle' And Michelle isn't a user_name but rather the name! Silly! Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted May 18, 2012 Share Posted May 18, 2012 This is why error handling is crucial. Use it always on development systems. Quote Link to comment Share on other sites More sharing options...
Craptacular Posted May 18, 2012 Author Share Posted May 18, 2012 I have updated my code to: delete_user.php: <?php require('config.php'); // Connect to Database to store information $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAMETWO); $names = mysqli_real_escape_string($mysqli, $_POST['name']); if (mysqli_connect_errno()) { printf("Connect Failed: %s\n", mysqli_connect_error()); } else { $sql = "DELETE FROM users WHERE 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 ?> and add_delete_users.php to: <?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 name FROM users WHERE (user_id <= '3' OR user_id >= '12')"; $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>"; } //This is the delete user form echo"<lable><strong>Delete User</strong></lable><form class=\"delete-form\" method=\"post\" action=\"delete_user.php\"><select name=\"name\" id=\"name\">\n" . $options . "</select> <input type=\"submit\" value=\"- Delete User\" /></form><br /><br />"; //This is the add form 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 it's still not working... I changed everything to reflect that I wanted to grab the name and base my SQL actions off of that data, ONLY... Arrrrg! Quote Link to comment Share on other sites More sharing options...
Illusion Posted May 19, 2012 Share Posted May 19, 2012 Did you try delete statement on users table from CUI or PhpMyAdmin and see what is happening ? Quote Link to comment Share on other sites More sharing options...
fenway Posted May 19, 2012 Share Posted May 19, 2012 Stop posting all of your code. Post your queries (raw queries) and mysql_error() output. Quote Link to comment Share on other sites More sharing options...
Craptacular Posted May 22, 2012 Author Share Posted May 22, 2012 Got it figured out! Thanks everyone! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.