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;
}
}
}