0

I get this error from the first line of the following code:

    Fatal error: Only variables can be passed by reference in /home/path/file.php on line 36



        if (isset($_POST['id']))
        {
            $ids = array_walk('intval', $_POST['id']);

            $sql = "DELETE FROM table WHERE id IN (' . implode(',', $ids) . ')')";
            //run query here

            $msg->type = "success";
            $msg->text = "Bulk delete has been successful";
        }

Any ideas what it could be?

BTW, the above code is to mass delete items.

Error is caused by $ids = array_walk('intval', $_POST['id']);

7
  • use double quotes in this line instead: "DELETE FROM table WHERE id IN (" . implode(',', $ids) . ")')"; Commented Mar 8, 2012 at 23:26
  • Do you have an empty value in one of your id's? Commented Mar 8, 2012 at 23:26
  • 1
    have you got the parameters on array_walk in the wrong order? Commented Mar 8, 2012 at 23:26
  • @AndreasStokholm The issue is with the $ids = array_walk('intval', $_POST['id']); line Commented Mar 8, 2012 at 23:27
  • @Scuzzy what do you mean? The $_POST['id'] is empty, I moved it inside the if statement (updated the Q), but I still get the error. Commented Mar 8, 2012 at 23:30

4 Answers 4

2

your call to the function array_walk is incorrect

bool array_walk ( array &$array , callback $funcname [, mixed $userdata = NULL ] )

try this instead and assuming that $_POST['id'] is an array

$ids = array_walk($_POST['id'], 'intval');

http://php.net/manual/en/function.array-walk.php

Also it may be good to check before calling the function to make sure that $_POST['id'] is an array

EDIT

After looking at what you doing a little more the function you need to use is array_map. array_walk returns a boolean while array_map returns an array which is what it looks like you need returned since you are using implode on $ids.

So you need to have

$ids = array_map('intval', $_POST['id']);
Sign up to request clarification or add additional context in comments.

Comments

1

array_walk calls a function for every array, and takes function as parameter 0. I think you need to change:

array_walk('intval', $_POST['id']); to array('intval', $_POST['id']);

Comments

0

Which line is line 36? Also, you seem to have an extra close parenthesis in the SQL statement. Furthermore, is the $_POST variable really an array?

1 Comment

yes it's an array. The html is <input type="checkbox" name="id[]" value="{$id[$index]}" />
0

Maybe you have changed the order of arguments in $ids = array_walk('intval', $_POST['id']);? In my opinion it should be $ids = array_walk($_POST['id'],'intval');.

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.