1

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");
        }
    }
3
  • Show where $id is coming from Commented May 13, 2011 at 12:17
  • 1
    Show the complete code. We cannot possibly know where line 15 is nor what's in there. Commented May 13, 2011 at 12:27
  • @Mustafa: Why are you trying to remove the error message and and retag a MySQL question as a T-SQL question? Commented May 13, 2011 at 12:30

5 Answers 5

2

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.

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

Comments

1

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

" LIMIT 1".

Comments

1

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.

5 Comments

@Adrian yes it is set. I get the error: 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
Did you try to insert the space before LIMIT?
@Adrian yes i did. Now the code goes through, but nothing deletes. Does it help if I say $id is an int?
Well, the $id should be an int, so you are doing it right in that regard. It doesn't get me any closer to figuring out the actual problem though...
BTW, did you look at PENDO's answer? That looks promising... try echoing out the $id from within the function to really make sure it's set...
1

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

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

? If so, change to ==.

3 Comments

@JD Audi: Please show what's on line 15, exactly. How are you calling the Accounts() function?
I just updated the question. I am submitting a hidden field saying "delacct" to a class that parses it.
I can see that. But the error message says that you're trying to use a function value on the left hand side of an assignment, and there's no assignments in the code you're showing. What is on your line 15? You have several people asking you that.
1

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.

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.