PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am trying to figure out why this delete script does not want to work. I click on the delete user and all it does it take me back to the page to pick a person to delete from the database.
When I run the sql in the database it deletes the user with no problem
This is the show_user_del.php page. This is the verification page to make sure you are deleting the right person
Here is the do_delete_user.php page. Obviously the page that actually does the deleting. As I mentioned before all it does is reroute back to the pick_user.php page. without removing the person from the database
<?php
require('../includes/auth_user.php');
$connection = mysql_connect($host, $username, $dbpassword) or die (mysql_error());
$db = mysql_select_db ($dbname, $connection) or die (mysql_error());
$sql = "SELECT * FROM $table";
if(!$connection) die("You do not have a valid SQL connection.");
$result = mysql_query($sql, $connection);
if(!$result) {
echo "<p>There was an error processing the following SQL statement.</p><p>".$sql."</p>";
}
//check the humber of results
$num = mysql_num_rows($result);
//if results are found loop through them and make a form selection block
if ($num < 1){
$display_block = "<p><em>Sorry! No results.</em></p>";
} else {
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$f_name = $row['f_name'];
$l_name = $row['l_name'];
$option_block .="<option value=\"$id\">$f_name $l_name</option>";
}
//create the entire form block
$display_block = "<FORM METHOD=\"POST\" ACTION=\"show_user_del.php\">
<p><strong>User Names:</strong>
<select name=\"id\">
$option_block
</select>
<input type=\"submit\" name=\"submit\" Value=\"Get User\"></p>
</form>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Classified Added</title>
</head>
<body>
<div>
<h2>Delete A User</h2>
<p>Select from the list below to delete the user.</p>
<?php echo "$display_block"; ?> <br />
<p><a href="../admin_menu.php">Administration Menu</a></p>
</div>
</body>
</html>
<?php
$sql = "SELECT * FROM $table WHERE id = '$_POST[id]'";
//
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
}
?>
<!-- append value of $id to the page -->
<form method="POST" action="do_delete_user.php?id=<?php $id; ?>">
You have to append the value of $id to your action attribute (see above) value (I'm guessing that the value of $id will be the same as $_POST['id']). On your do_delete_user page modify your code like this
$sql = "DELETE FROM $table WHERE id='" . mysql_real_escape_string($_GET['id']) . "' ";
Now the value of $_GET['id'] is retrieved from the query string and your record should be deleted. Make sure you check the value of $_GET['id'] and use mysql_real_escape_string() on all input values.
Hth.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
<?php
$sql = "SELECT * FROM $table WHERE id = '$_POST[id]'";
//
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
}
?>
<!-- append value of $id to the page -->
<form method="POST" action="do_delete_user.php?id=<?php $id; ?>">
You have to append the value of $id to your action attribute (see above) value (I'm guessing that the value of $id will be the same as $_POST['id']). On your do_delete_user page modify your code like this
$sql = "DELETE FROM $table WHERE id='" . mysql_real_escape_string($_GET['id']) . "' ";
Now the value of $_GET['id'] is retrieved from the query string and your record should be deleted. Make sure you check the value of $_GET['id'] and use mysql_real_escape_string() on all input values.
Hth.
I copied and pasted what you wrote and I get the same result