Click to See Complete Forum and Search --> : Code not deleting user.


R.Noon
12-14-2006, 07:32 PM
Right... I have the following code...

It doesn't work, once I actually submit it and say yes to delete; it just goes back to the original screen where it chooses between names.

<?PHP
include('header.php');
if($username!=NULL)
{
if((!isset($_POST['submit'])) || (isset($_POST['submit2'])))
{
$SQL_TEST = "SELECT `name` FROM `user`";
$result = mysql_query($SQL_TEST) or die(mysql_error());
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_assoc($result))
{
$names[] = $row['name'];
}
}
else
{
echo 'query returned no results';
}
echo "Which of the following names would you like to delete?";
echo "<br />------------------------------------------------------";
foreach($names as $name)
{?>
<form method="post" action="">
<?PHP
echo "$name"; ?> <input type="radio" name="delet" value="<? echo $name; ?>" />
<br /><?PHP
}?><input type="submit" value="Delete" name="submit" /></form><?PHP
}
else if((!isset($_POST['submit2'])) || (!empty($sub2_set)))
{
$delUser = $_POST['delet'];
$sql_statDisp = "SELECT * FROM `user` WHERE `name`='" . $_POST['delet'] . "'";
$result2 = mysql_query($sql_statDisp) or die(mysql_error());
echo "Are you sure you want to delete user ", $delUser , "?";?>
<form action="" method="post">
Yes <input type="radio" name="del" value="Yes">No <input type="radio" name="del" value="No">
<input type="submit" name="submit2" value="Submit">
</form><?PHP
echo "<br /> Below are ", $delUser , "'s Stats <br />";
$sql_displayStats = "SELECT * FROM `user` WHERE `name`='". $_POST['delet'] ."'";
$stat_query = mysql_query($sql_displayStats) or die(mysql_error());
echo $stat_query;?>
<?PHP }
if(isset($_POST['del']))
{
if($_POST['del'] == "Yes")
{
echo $delUser;
$sqlDel = "DELETE FROM `user` WHERE `name` = '" . $delUser . "'";
$result3 = mysql_query($sqlDel);
}
else
{
echo "User not deleted.";
}
}
}
include('footer.php');
?>

ava
12-15-2006, 09:16 AM
A few things...

First of all, why are you constantly switching between php and html? It's a little distracting and unnecessary. Also, you can simply place php-variables inside an echo (providing it's between double quotes), like "My name is $Name"

But I think the problem is with the query. Try omitting the single quotes around the table name and the field name like this:
$sqlDel = "DELETE FROM user WHERE name = '$delUser'";

On a sidenote, I'm not sure what you think the $username!=NULL will do. $username is not defined anywhere (unless it's in the header.php) and checking for an empty $username is usually done with !empty($username) or isset($username) depending on how you feel about empty strings or 0-values.

If you can't solve it with this, make sure the user you connect to the database with has writing privileges. If that's not it either, give the header.php code and I'll look at it again.

NightShift58
12-16-2006, 03:50 AM
Ava is right when s/he suggests that your code is a little distracting.

Your problem is that by the time you're actually executing the DELETE query, your script no longer knows what $delUser stands for. You're using one script which calls itself 3 times. But at the critical moment, you're not passing that value to the part of the script which will do the deleting.

When you ask the user:
echo "Are you sure you want to delete user ", $delUser , "?";
you follow that with a <form> where one can confirm his intentions. However, nowhere on that <form> do you "remember" what user it is that should be deleted. As such, when you get to the final stage, that variable is lost and your script is actually trying to delete an empty name.

One of the possible solutions would be to include a hidden input field in the <form> used to confirm the delettion:
<input type='hidden' name='delet' value='{$_POST['delet']}>

Then in the final stage, you would need to assign the value of $_POST['delet']
to the variable $delUser, that is used in the DELETE query.

That should fix your immediate delete problem. But you do need to do work on the cosmetics, as Ava mentioned, and you need to check on the function addslashes(), which you will definitely need the moment you want to insert or delete any name like "O'Hare", as your script will crash...