3 Replies - 25848 Views - Last Post: 24 November 2008 - 11:43 AM Rate Topic: -----

#1 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Delete Row From Mysql

Posted 24 November 2008 - 02:04 AM

This is easy isn't it? Then why can't I do it. The problem I have is deleting a row from my Database(MySQL) which I have been able to do. But now I am displaying the users in my database and trying to delete the ones I don't want. So to do this I display them with a form which has a delete button for each one.
The button works as it keeps telling me that the account has been deleted even though it is still there. So I think its not sending the data. Here is my code for this if you can see the problem that would be great as to me it looks right.

if (isset($_POST['delete'])) {
$user = $_POST['user'];
$sql = "DELETE FROM users WHERE user_name = $user";
$result = mysql_query($sql);

echo "Account has been deleted.";
}		

$sql = mysql_query("SELECT * FROM users ORDER  by full_name ASC")or die(mysql_error());
   
  
while($row = mysql_fetch_array($sql))
{ 
$user = $row['user_name'];
  echo "<table width='650px'>";
  echo "<tr class='rowA'><td>";
  echo "<b>User:</b> ";
  echo $row[user_name];
  echo "</td></tr>";
  echo "<tr class='rowB'><td>";
  echo "<b>Location:</b> ";
  echo $row[country];
  echo "</td></tr>";
  echo "</br>";
 
?>

<form action="users.php" method="post">
<table border="0">
<input type="hidden" name="user" maxlength="60" value = "<?php echo $row['user_name']; ?>">
<tr><td colspan="2" align="right">
<input type="submit" name="delete" value="Delete User">
</td></tr>
</table>
</form>
<?

}
echo "</table>";
?>


Cheers in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Delete Row From Mysql

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Delete Row From Mysql

Posted 24 November 2008 - 03:34 AM

Replace this:

$sql = "DELETE FROM users WHERE user_name = $user";


With this:

$sql = sprintf("DELETE FROM users WHERE user_name = '".$user."'");


And that should make it work fine. It's reading it as deleting the user named '$user', not the value of $user.
Was This Post Helpful? 0
  • +
  • -

#3 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Delete Row From Mysql

Posted 24 November 2008 - 03:47 AM

Thank you for that it worked perfectly.
Was This Post Helpful? 0
  • +
  • -

#4 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: Delete Row From Mysql

Posted 24 November 2008 - 11:43 AM

also another thing i found out not long ago although i still do it my way is


$sql = "DELETE FROM users WHERE user_name = '$user'";

will work the same as
$sql = "DELETE FROM users WHERE user_name ='". $user."'";

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1