JD Audi
JD Audi

Reputation: 1055

MySQL Delete Item from Table not working

I am trying to delete a record in my db based on the unique id ($id). Is there something wrong with this code? Probably a simple one for you php pro's.

    function delAccount(){  

         mysql_query("DELETE FROM accounts WHERE id=".$id."LIMIT 1");

        }

I get a :

    Fatal error: Can't use function return value in write context in     
    /home/content/53/7311353/html/cca/accounts/include/processAct.php on line 15

My Class that I have powering everything:

    class Accounts
        {

    function Accounts(){
        if (isset($_POST['addacct'])){
            $this->addAccount();
        }elseif(isset($_POST['editacct'])){
            $this->editAccount();
        }elseif(isset($_POST['delacct'])){
            $this->delAccount();
        }else{
            // redirect if loaded without a POST value set
            header("Location: ../index.php?o=illegal&t=nodata");
        }
    }

Upvotes: 1

Views: 2333

Answers (5)

Pekka
Pekka

Reputation: 449395

Some hints on how to debug stuff like this.

  • If you suspect something is wrong, the first thing to do is to output the generated query. Like so:

    $query = "DELETE FROM accounts WHERE id=".$id."LIMIT 1";
    echo $query; // for debugging
    

    That will show you that at least one thing is wrong with your query: You have a space missing before LIMIT.

  • mysql_query() returns false if it encounters an error. You can check for that, and output it using mysql_error(). Like so:

    $result = mysql_query($query);
    if(!$result) trigger_error("Database error!: ".mysql_error());
    
  • If $id comes from outside, like the $_GET array, make sure you have tested whether it is an integer before using it in a query to avoid SQL injection.

Upvotes: 1

Joshua - Pendo
Joshua - Pendo

Reputation: 4371

You should, first of all, put a space between ".$id." and LIMIT so:

mysql_query("DELETE FROM accounts WHERE id=".$id." LIMIT 1");

Secondly, the $id is NOT available within this function by default. Either do this:

function delAccount($id) {  
  mysql_query("DELETE FROM accounts WHERE id=".$id." LIMIT 1");
}

and use delAccount($id_parameter); in your script to send the ID along with the function. Or try this:

function delAccount() {  
  global $id;
  mysql_query("DELETE FROM accounts WHERE id=".$id." LIMIT 1");
}

then you can call this function after you set the value of $id somewhere else in your code.

Upvotes: 2

LHMathies
LHMathies

Reputation: 2434

Your error is from the PHP compiler. Are you doing something like this on line 15:

if (delAccount(...) = false) { ... }

? If so, change to ==.

Upvotes: 1

Adrian Schmidt
Adrian Schmidt

Reputation: 1885

Are you sure $id is set?

If $id should be sent to the function as an argument, try this:

function delAccount($id) {
    mysql_query("DELETE FROM accounts WHERE id=" . $id . " LIMIT 1");
}

EDIT: You missed a space character between the ID and the LIMIT.

Added some small improvements to the form of the query string:

function delAccount($id) {
    mysql_query("DELETE FROM `accounts` WHERE `id` = " . $id . " LIMIT 1");
}

EDIT:

The error you get doesn't come from MySQL itself. Have you checked the returned value. It might return another error, or the returned value might be correct, but used in an erroneous way in later code.

Upvotes: 1

Richard H
Richard H

Reputation: 39055

First: is the value for $id actually an id in the database? Second you need a space before "LIMIT", ie:

" LIMIT 1".

Upvotes: 1

Related Questions