0

Why is my delete query not deleting data from database?

This is the code:

<?php
include ("includes/database.php");
session_start();

if(!$_SESSION['username']){

    header('location:index.php?error=You are not an administrator');
}else{
    $delete_id=$_GET['id'];
    $user=$_SESSION['username'];
    $delete='delete from `wall` where `ID`="'.$delete_id.'" AND `User_id`="'.$user.'" ';
    $Query=mysql_query($delete) or die (mysql_error()); 
    if ($Query){
        header('location:User_page.php');
    }
}
?>

The query is working because I do a redirect to User_page.php, but the data from database is not deleted.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • 2
    You're comparing `User_id` with username in `$user`? – vee Apr 03 '14 at 18:47
  • What happens if you `echo $delete;` and leave out the redirect? That's Debugging 101 in situations like this: Make sure the query is what you expect it to be. – Kromey Apr 03 '14 at 18:47
  • 1
    Lovely [SQL injection attack](http://bobby-tables.com). Enjoy having your server pwn3d. – Marc B Apr 03 '14 at 18:48
  • If the query is valid, it won't return false, even if it doesn't "do" anything. 1) Change "delete from" to "select * from" and check if it finds anything. (It won't since it didn't delete) 2) Check mysql_affected_rows() > 0 to see if anything changed. – cwurtz Apr 03 '14 at 18:52
  • 3
    Very constructive comment Marc. Bravo with your "help" – mathius1 Apr 03 '14 at 18:53
  • Write after `$delete=...`, `echo $delete; die();`, and check is that query correct. – kxc Apr 03 '14 at 18:55
  • echoing $delete just show me the query i've given :( – user3495125 Apr 04 '14 at 12:52
  • query is as it should be. the db connection is ok! i don't know why it can't delete the row from database :/ – user3495125 Apr 04 '14 at 12:58

1 Answers1

0

The redirect works if the query doesn't have an error, it doesn't mean the query changed anything. Try: mysql_affected_rows()

Also are you supposed to be using the $delete_id variable?

Community
  • 1
  • 1
mathius1
  • 1,381
  • 11
  • 17