1

I have some mysql_query()s that work just fine, but if I put them in an if else statement they return false. Any idea?

        public function delete_post() {

        $id = $_GET['post_id'];

        mysql_query("DELETE FROM posts WHERE post_id='$id' LIMIT 1");

        if ( !mysql_query() ) {
            echo ('Could not be Deleted');
        } else { 
            echo 'Deleted Successfully'; 
        }
    }

and when i run it, it deletes the post, but returns "Could not be deleted"

Andor Nagy
  • 174
  • 4
  • 15

4 Answers4

3

You're running a query with an empty SQL statement, which is not correct SQL. Try this.

$result = mysql_query("DELETE ...");
if (!$result) {
    echo ('Could not be Deleted');
} else {
    echo 'Deleted Successfully';
}

The difference is on line 2 (of my code).

mbarlocker
  • 1,310
  • 10
  • 16
2

Note: mysql_query() is deprecated. You really should use PDO::query instead.

If you still wish to use it, do:

$result = mysql_query("DELETE FROM posts WHERE post_id='$id' LIMIT 1");
if (!$result) 
{
    echo ('Could not be Deleted');
}

Explanation:

In you original code you call mysql_query() two times. The second time, there is no argument, so it doesn't work, and that's what your code is reporting.

Jean
  • 7,623
  • 6
  • 43
  • 58
1

I think this is a pretty similar question / answer: What does a successful MySQL DELETE return? How to check if DELETE was successful?

Can you try what it suggests and see if that will work for you?

Community
  • 1
  • 1
anotherBob
  • 33
  • 10
0

You could try :

public function delete_post() {

        $id = $_GET['post_id'];

        $query = mysql_query("DELETE FROM posts WHERE post_id='$id' LIMIT 1");

        if ( !$query ) {
            echo ('Could not be Deleted');
        } else { 
            echo 'Deleted Successfully'; 
        }
    }
RelevantUsername
  • 1,270
  • 8
  • 14