Not delete specified content from mysql

201 Views Asked by At

I've recently made a PHP, that should; if click a link delete a certain row within one of my MYSQL tables.

The script below has everything but the link [href=delete_ac.php?id etc...] leads to the page but when the page activates it echo ERROR instead of deleting the row.

<h1>Members</h1> 
<table> 
    <tr> 
        <th>ID</th> 
        <th>Username</th> 
        <th>E-Mail Address</th> 
        <th></th>
    </tr> 
    <?php foreach($rows as $row): ?> 
        <tr> 
            <td><?php echo $row['id']; ?></td> 
            <td><?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8'); ?></td> 
            <td><?php echo htmlentities($row['email'], ENT_QUOTES, 'UTF-8'); ?></td> 
            <td><a href="delete_ac.php?id=<?php echo $row['id']; ?>">delete</a></td> 
        </tr> 
    <?php endforeach; ?> 
</table>

delete_ac.php The script below is what should delete it but it isn't

<?php

    require("../php/bp-connectionAdmin.php");

    $id=$_GET['id'];

    $query = "DELETE FROM `users` WHERE `id` = $id";
    $result = mysql_query($query);

   if ($result) {
        echo "Successful";
   } else {
        echo "ERROR";
   }
?> 
2

There are 2 best solutions below

0
On BEST ANSWER

Is the ID numeric only? Would the addition of quote marks around $id not help?

$query = "DELETE FROM `users` WHERE `id`='$id'";
mysql_query($query);

Not sure...but give it a go!

0
On

Put on the line after $query = "DELETE ..

An

echo "DELETE FROM `users` WHERE `id` = $id";
die;

Then you will see what goes wrong. Personally i would remove the ', assuming that the id=integer, and you will have:

$query = "DELETE FROM users WHERE id=$id";

If not, try that echood query directly in your Database window and you will see what is wrong.

Most probably you should change your line into

 $id=intval($_GET['id']);

which is also much more secure!