Jump to content

why this isn't working?


Craptacular

Recommended Posts

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=\"&#45; 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=\"&#43; 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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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=\"&#45; 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=\"&#43; Add User\" /></form>";




mysqli_close($mysqli);				

?>

</div>
</div>
</div>
</body>
</html>

 

Thanks

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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!

Link to comment
Share on other sites

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=\"&#45; 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=\"&#43; 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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.