Jump to content

Recommended Posts

Hello Everyone,

 

I am writing a simple CMS system for someone. I am having trouble getting the delete function to work. I pick the user that I want to remove from the system. I verify that this is the right person to remove. Then the record is deleted from the database.

 

The part that picks the person and verifies that it is the right person to remove from the database work fine. It is the part that actually deletes the record. It takes me back to the pick_user.php.

 

If someone could help me with this I would appreciate it

 

This is the code from the show_user_del.php

<?php

if (!$_POST[id]) {
header ("LOCATION: pick_user.php");
exit;
}

if ($_COOKIE[auth] != "ok") {
header("Location: ../login.php");
}

require('../includes/auth_user.php');

//build and issue query
$sql = "SELECT * FROM $table WHERE id = '$_POST[id]'";
$result = mysql_query($sql, $connection) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
$f_name = $row['f_name'];
$l_name = $row['l_name'];
$username = $row['username'];
$password = $row['password'];
}

?>

<!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>Add Classified Ad</title>
</head>
<body>
<h2><em>Delete user from Database</em></h2>
<h3>User being deleted <?php echo "$f_name $l_name"; ?></h3>
<form method="POST" action="do_delete_user.php">
<input type="hidden" name="id" value="<?php echo "$_POST[id]"; ?>" />
<input type="hidden" name="f_name" value="<?php echo "$f_name"; ?>" />
<input type="hidden" name="l_name" value="<?php echo "$l_name"; ?>" />
<p> <strong>Name:</strong> <?php echo "$f_name $l_name"; ?>
   </p>
   <p> <strong>Username:</strong> <?php echo "$username"; ?>
   </p>
   <p> <strong>Password:</strong> <?php echo "$password"; ?>
   </p>
   <p>
      <input type="submit" name="submit" id="name" value="Delete User" />
   </p>
</form>
<p><a href="../admin_menu.php">Return to Administration Menu</a></p>
</body>
</html>

 

When the submit button is clicked in the above code. It is supposed to bring you to the page below saying that the record was deleted. When the submit button is clicked it sends you back to the pick user page.

 

<?php
if ((!$_POST[f_name]) || (!$_POST[l_name])) {
header ("LOCATION: show_user_del.php");
exit;
}

if ($_COOKIE[auth] != "ok") {
header("Location: ../login.php");
}

require('../includes/auth_user.php');

$sql = "DELETE FROM $table WHERE id = '$_POST[id]'";
$result = mysql_query($sql, $connection) or die(mysql_error());
?>

<!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>Untitled Document</title>
</head>

<body>
<h1>User has been removed</h1>
<h2><em>User <?php echo "$_POST[f_name] $_POST[l_name]"; ?> has been deleted
      from the <?php echo "$table"; ?> table</em></h2>
<p><a href="pick_user.php">Delete another person</a></p>
<p><a href="../admin_menu.php">Administration Menu</a></p>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/226538-cant-get-delete-function-to-work/
Share on other sites

$sql = "DELETE FROM $table WHERE id = '$_POST[id]'";$result = mysql_query($sql, $connection) or die(mysql_error());

 

you need to define the name of the table you are deleting the user id from.

 

then:

WHERE id='$id'

instead of the post id.

 

 

Hopefully that works for you.

Have you echoed the query string to be sure it contains the values you'd expect it to contain? If it did, what happens if you paste that output into phpMyAdmin and execute the query that way?

I tried that and at first it did not work

 

I went back and removed the section of code from the script

 

if ((!$_POST[f_name]) || (!$_POST[l_name])) {
header ("LOCATION: show_user_del.php");
exit;
}

if ($_COOKIE[auth] != "ok") {
header("Location: ../login.php");
}

 

it then worked perfectly fine with no problems.

OK... I have no idea what is going on. It is back to not working. I will have to play around with it when I get home from work tomorrow and see what is up

 

I also tried running the query in myadmin and it worked.

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.