Forum Moderators: coopster

Message Too Old, No Replies

can't delete a user from a mysql table

         

99naa

3:35 am on Apr 22, 2006 (gmt 0)

10+ Year Member



Hi I am having trouble deleting a user for a table. UserKey is the primary key in the table.

Here is the code
<html>
<head></head>
<body>
<?php
//Display's all users
include ("connect.php");
$query = "select * from users;";
//echo "$query";
$result = mysql_query($query)or die ("error");
echo "<table border = '1' align ='center'>";
echo "<tr><td>Users</td><td>Action</td></tr>";

while ($row = mysql_fetch_array($result))
{
echo "<tr><td>".stripslashes($row['User'])."</td>";
echo '<td>&nbsp;&nbsp;<a href="edit_user.php?$id='.$row["UserKey"].'& User='.$row["User"].'">Edit </a>';
echo '&nbsp;&nbsp;&nbsp;<a href="delete_user.php?$id='.$row["UserKey"].'&User='.$row["User"].'"> Delete</a></td></tr>';

}
echo "</table>";
mysql_close();
echo "<br><a href = 'manage_user.php'>Return to Manage User Page</a>";
?>
</body>
</html>

and the second part is

<html>
<head><title>Delete Books</title><head>
<body>
<?php
//Declare the variables
$id=$_GET['id'];
$User=$_GET['User'];
$submit=$_POST['submit'];
$UserKey=$_GET['UserKey'];

if (!$submit)
{
echo '<table border=1>';
echo '<tr><td colspan=2><br>&nbsp;Do you wish delete <b>
'.$User.' </b> from table <b>users</b>?&nbsp;<p></p></td></tr>';
echo '<tr><td align=center><br>';
echo '<form action="delete_user.php" method="POST">';
echo '<input type="hidden" name="UserKey" value="'.$id.'">';
echo '<input type="submit" name="submit" value="Yes">';
echo '</form>';
echo '</td>';
echo '<td align="center"><br>';
echo ' <form action="delete_user.php" method="POST">';
echo ' <input type="submit" name="submit" value=" No ">';
echo ' </form>';
echo '</td></tr>';
echo '</table>';
}
echo "$id";
//echo "$UserKey";
if ($submit == "Yes") {
include("connect.php");
$query = "DELETE FROM users WHERE (UserKey ='$UserKey')";
$result = mysql_query($query) or die("$User was not deleted. " . mysql_error());
//$result = mysql_query ($query);

echo ". $User. is deleted";
}
if ($submit == " No ") {
echo 'User Not Deleted';
}
?>
<br><br><a href="display_user.php">Display Users</a>
</body>
</html>

I have been getting this error message

was not deleted. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

I have got to the stage where I don't know what I am doing or where I am going wrong.

Any help would be gratefully received.

99naa

eelixduppy

4:25 am on Apr 22, 2006 (gmt 0)



Hello...

in this section of your code:

while ($row = mysql_fetch_array($result))
{
echo "<tr><td>".stripslashes($row['User'])."</td>";
echo '<td>&nbsp;&nbsp;<a href="edit_user.php?$id='.$row["UserKey"].'& User='.$row["User"].'">Edit </a>';
echo '&nbsp;&nbsp;&nbsp;<a href="delete_user.php?$id='.$row["UserKey"].'&User='.$row["User"].'"> Delete</a></td></tr>';

}

you have $id in the url instead of just 'id'. You should remove the '$' to make that whole section look like this:

while ($row = mysql_fetch_array($result))
{
echo "<tr><td>".stripslashes($row['User'])."</td>";
echo '<td>&nbsp;&nbsp;<a href="edit_user.php?id='.$row["UserKey"].'& User='.$row["User"].'">Edit </a>';
echo '&nbsp;&nbsp;&nbsp;<a href="delete_user.php?id='.$row["UserKey"].'&User='.$row["User"].'"> Delete</a></td></tr>';

}

Also you forgot the close mysql in your second file so just add that, which doesn't make much of a difference.

Other than this, i've noticed that $user doesn't seem to have a value in your error message. With this i would just check to make sure that it is present by echoing it to the screen or checking to see if it is in the url.

Hope this helps...

99naa

4:58 am on Apr 22, 2006 (gmt 0)

10+ Year Member



Hi

Thank you for your help. I have made the suggested corrections. Its still not working. Still getting the same error message. I am hoping that I can ask you a couple of more questions.

I have echoed out the $User, it is showing up in the url but not in anywhere else, how can I fix this?

sorry but I am way out of my depth.

Many thanks
99nna

eelixduppy

5:38 am on Apr 22, 2006 (gmt 0)



Hi again....

Im still having trouble figuring out why your query isn't working...I believe it has to do with what values you are assigning $UserKey, which is producing the error message. Can you please give me the example $UserKey that you are using in order for me to understand exactly whats going on.

Also you want to get rid of the whitespace before User to produce this, otherwise User won't be displayed

echo '<td>&nbsp;&nbsp;<a href="edit_user.php?$id='.$row["UserKey"].'&User='.$row["User"].'">Edit </a>';

eelix

also, just a note, you are making yourself crazy with all of the (")'s and (')'s in your code. just to make your code a little easier to read and write.

This:

$fish = "yum";
print "I love fish. $fish";

Produces the same results as this:

$fish = "yum";
print "i love fish. ".$fish;

It just looks neater, i think

99naa

6:25 am on Apr 22, 2006 (gmt 0)

10+ Year Member



Hi there

Thank you for the tips on the quotes I will go through and re look at those. I am not much of a coder at all.

Here is an example of the table that I am using

Password User UserKey

Yvonne Yvonne 1
Sue Sue 2
Fred Fred 3
John John 4

Unfortunately I am stuck with the column names.

In some ways I am glad that it is not an obvious error, but it is still frustrating. I will retype the code and see if that gets rid of the problem as well.

Thank you so much for your help. I hope that I have answered your question correctly.

99naa