0, // 'message' => 'Statistics Added Successfully!' // ]; // echo json_encode($response); // } else { // $response = [ // 'error_flag' => 1, // 'message' => 'Failed to add statistics details.' // ]; // echo json_encode($response); // } // } else { // $response = [ // 'error_flag' => 1, // 'message' => 'Invalid request. Missing required parameters.' // ]; // echo json_encode($response); // } require_once 'db.php'; if (isset($_POST['title']) && isset($_POST['counter'])) { // Extract data from the POST request $title = mysqli_real_escape_string($link, $_POST['title']); $counter = mysqli_real_escape_string($link, $_POST['counter']); $parameter = mysqli_real_escape_string($link, $_POST['parameter']); // Check the current count of statistics $countQuery = "SELECT COUNT(*) AS total FROM statistics"; $countResult = mysqli_query($link, $countQuery); $countRow = mysqli_fetch_assoc($countResult); $totalStatistics = (int)$countRow['total']; if ($totalStatistics < 4) { // If less than 4 statistics, proceed with insertion $sql = "INSERT INTO statistics ( statistics_id, title, counter, parameter) VALUES ('', '$title', '$counter', '$parameter')"; if (mysqli_query($link, $sql)) { $response = [ 'error_flag' => 0, 'message' => 'Statistics Added Successfully!' ]; echo json_encode($response); } else { $response = [ 'error_flag' => 1, 'message' => 'Failed to add statistics details.' ]; echo json_encode($response); } } else { // If already 4 statistics, send an error response $response = [ 'error_flag' => 1, 'message' => 'You cannot add more than 4 statistics.' ]; echo json_encode($response); } } else { $response = [ 'error_flag' => 1, 'message' => 'Invalid request. Missing required parameters.' ]; echo json_encode($response); }