0

I'm working on a fairy simple saving class using the below code, whenever I run it, only the first if statement is triggered one time, the data is saved in the logs_all table once and once only. All variables passed to the function fine.

I have spent an hour debugging and have honestly no idea what the problem is, could anyone assist?

function saveLog($logtype, $user, $category, $department, $item, $action) {

            $logtype = 'all,buying';
            $a = explode(",",$logtype);

            foreach($a as $value) {

                if($value == 'all') {

                    $db = new mysqli(DB_HOST,DB_USERNAME, DB_PASSWORD, DB_NAME);
                    $sql = "INSERT INTO logs_all (user, category, department, item, action, date) VALUES ('$user', '$category', '$department', '$item', '$action', NOW())";
                    $db->query($sql);
                    return $db->insert_id;


                }

                if($value == 'buying') {

                    $db = new mysqli(DB_HOST,DB_USERNAME, DB_PASSWORD, DB_NAME);
                    $sql = "INSERT INTO logs_buying (user, category, department, item, action, date) VALUES ('$user', '$category', '$department', '$item', '$action', NOW())";
                    $db->query($sql);
                    return $db->insert_id;


                }

            }

}
2
  • please create plunker for it Commented Sep 9, 2014 at 15:28
  • 1
    From the awesome PHP docs: "If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call." Commented Sep 9, 2014 at 15:29

3 Answers 3

2
return $db->insert_id;

This is your problem. The return statement returns out of the context of the function, or in a global case, the PHP script entirely. This means the foreach stops executing along with any other PHP code inside the function.

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

2 Comments

Thanks, Must have stared at it for an hour and missed something so rudimentary
@user1372212 No problem! Please make sure you mark the answer that you feel has best answered your question. You can do so by clicking on the grey checkmark icon below the up/down arrows so that it turns green.
2

It's because your are using a return. From php.net:

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.

Your function is terminating once it hits that return. If you want to return each item, it would be better to save the ids to an array, then return the array after the loop has finished.

Comments

1

You are quitting from the function the first time your code reaches return.

Here's a simplified version of your code, which makes a use of that your logtype has the same name as the table name in the database:

function saveLog($logtype, $user, $category, $department, $item, $action) {
    $logtype = 'all,buying';
    $a = explode(",",$logtype);
    $result = array();

    foreach($a as $value) {
        $db = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
        $sql = "INSERT INTO logs_" . $a . " (user, category, department, item, action, date) VALUES ('$user', '$category', '$department', '$item', '$action', NOW())";
        $db->query($sql);
        $result[] = $db->insert_id;
    }
    return $result;
}

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.