1

I'm trying to make a little control panel so I can add and delete rows in a database. I've already made the adding part work but I'm having some trouble with the deleting.

The page shows every row in a table and I've added a delete button behind it.

This are the codes I have

This is the code that makes the table:

<table class='col-sm-6 fixed_headers'>
<thead>
<tr>
<th>Username</th>
<th>Password</th>
<th>Email</th>
</tr>
</thead>
<tbody style='height:250px; overflow:auto;'>";

while($row = mysqli_fetch_array($result))
{
 "<tr class=". $row['ID'] . ">";
 echo "<td>" . $row['uname'] . "</td>";
 echo "<td>" . $row['pass'] . "</td>";
 echo "<td>" . $row['email'] . "</td>";
 echo "<td><input type='submit' class='button' id=".$row['ID'] . " value='del'></td>";
 echo "</tr>";
 }
echo "</tbody>
 </table>";
?>  

Here is the ajax code:

<script type="text/javascript">
$(document).ready(function(){
$('.button').click(function(){
    var btnID = $(this).id;


  jQuery.ajax({
        type: "POST",
        url: "php/delete.php",
        data:  {id :btnID}
    }).done(function(result){
    if(result== "succes"){
        $('#error').remove();
        $('#succes').html("Het account is verwijderd. ");

    }else {
        $('#error').empty();
        $('#error').html("Er is een fout opgetreden.<br/> Probeer opnieuw! ");
    }
    });
  });

});
</script>

and here is the delete.php:

$id =  mysql_real_escape_string($_POST['id']);

$delete = "DELETE FROM `xxx`.`xxx` WHERE `xxx`.`ID` = '$id'"; 
$del = mysql_query($delete);

if ($del)
{
    echo "succes";

}
else
{
    echo "error";
    // close connection 
    mysql_close();
}
?>

when I press the delete button, I do get the succes message but the row hasn't been deleted of my database.

I have been trying to fix this, but I can't seem to make it work

(left out my db connect code)

2
  • what is xxx.xxx is it a table or a column xxx in a table xxx ? Commented Jul 27, 2014 at 12:10
  • it's the name of the table Commented Jul 27, 2014 at 12:18

2 Answers 2

2

My Advice to you, is to NEVER delete from your database.

Instead, add a boolean variable to your table, which is by default set to '1' and whenever you want to "delete" a row, Set your bool to '0'.

When you query your table to show on your page, query WHERE boolVariable = '1'.

For security reasons, this is the best approach, and make sure to remove the "delete" priviledge from the Db user you're using to connect to your database


One more thing, stop using mysql_query, and use mysqli_query as mysql_ is deprecated

source: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

Sign up to request clarification or add additional context in comments.

3 Comments

thx for the advice! Do you also know what is wrong with the code above?
Did you forget to add ';' at the end of your sql query ? $delete = "DELETE FROM xxx.xxx WHERE xxx.ID = '$id';";
@RalphMelhem : you can do that $id';, but is not necessary
1

Changing $(this).id to $(this).attr('id') fixed it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.