There is no such thing as ON DUPLICATE DELETE or the proper use of IF ELSE with subqueries to do this in a single query (which would be convenient since this single query would be either successful or unsuccessful). Think that you will need to fulfill several requests to achieve your goal, just like me. But keep track of whether subsequent requests continue.
Start with the return value ("state" in this case), which is "Unknown" or "NoChange". If this returns you an ajax call, you know that the flag should return to its previous state before clicking and maybe a pop-up message (which you can carry along with json).
run your queries in the correct if / then structure and keep track of subsequent queries and update the βstateβ when the series has succeeded accordingly. This should do exactly what you need, to prevent the presence of black holes.
(I use some wrappers to execute queries that return true / false / null / errorinformation - which allows you to check every query result)
I assume you already have your ajax call. The code example starts with your ajax call starting processing from the server side:
ob_end_clean(); $json = array("state" => "Unknown"); $module_id = (isset($_POST['module_id']) ? $_POST['module_id'] : null); $group_id = (isset($_POST['group_id']) ? $_POST['group_id'] : null); $action = (isset($_POST['action']) ? $_POST['action'] : null); if (!empty($module_id) && !empty($group_id) && !empty($action)) { $query = " SELECT 1 FROM config_module_actions_groups WHERE 1 AND module_id = '{$module_id}' AND group_id = '{$group_id}' AND action = '{$action}' "; if (getQueryAsArraySingleValues($query)) { $query = " DELETE FROM config_module_actions_groups WHERE 1 AND module_id = '{$module_id}' AND group_id = '{$group_id}' AND action = '{$action}' "; if (executeQuery($query)) { $json["state"] = "Off"; } else { $json["message"] = "Not correctly deactivated"; } } else { $query = " REPLACE INTO config_module_actions_groups SET module_id = '{$module_id}', group_id = '{$group_id}', action = '{$action}' "; if (executeQuery($query)) { $json["state"] = "On"; } else { $json["message"] = "Not correctly activated"; } } } // output echo json_encode($json); exit;
source share