0) { $error = "Test code already exists!"; } else { $query = "INSERT INTO tests (test_name, test_code, category_id, description, sample_type, methodology, price, turnaround_time) VALUES ('$test_name', '$test_code', '$category_id', '$description', '$sample_type', '$methodology', '$price', '$turnaround_time')"; if(mysqli_query($conn, $query)) { $success = "Test added successfully!"; } else { $error = "Error: " . mysqli_error($conn); } } } // Update Test if(isset($_POST['update_test'])) { $id = $_POST['test_id']; $test_name = mysqli_real_escape_string($conn, $_POST['test_name']); $test_code = mysqli_real_escape_string($conn, $_POST['test_code']); $category_id = $_POST['category_id']; $description = mysqli_real_escape_string($conn, $_POST['description']); $sample_type = mysqli_real_escape_string($conn, $_POST['sample_type']); $methodology = mysqli_real_escape_string($conn, $_POST['methodology']); $price = $_POST['price']; $turnaround_time = mysqli_real_escape_string($conn, $_POST['turnaround_time']); $query = "UPDATE tests SET test_name='$test_name', test_code='$test_code', category_id='$category_id', description='$description', sample_type='$sample_type', methodology='$methodology', price='$price', turnaround_time='$turnaround_time' WHERE id=$id"; if(mysqli_query($conn, $query)) { $success = "Test updated successfully!"; } } // Delete Test if(isset($_GET['delete'])) { $id = $_GET['delete']; mysqli_query($conn, "DELETE FROM tests WHERE id = $id"); header("Location: tests.php?msg=deleted"); exit(); } // Toggle Status if(isset($_GET['toggle_status'])) { $id = $_GET['toggle_status']; mysqli_query($conn, "UPDATE tests SET status = NOT status WHERE id = $id"); header("Location: tests.php"); exit(); } // Get all tests with category $search = isset($_GET['search']) ? mysqli_real_escape_string($conn, $_GET['search']) : ''; $category_filter = isset($_GET['category']) ? $_GET['category'] : ''; $where = "WHERE 1=1"; if($search) { $where .= " AND (t.test_name LIKE '%$search%' OR t.test_code LIKE '%$search%' OR t.sample_type LIKE '%$search%')"; } if($category_filter) { $where .= " AND t.category_id = '$category_filter'"; } $tests = mysqli_query($conn, "SELECT t.*, tc.category_name FROM tests t LEFT JOIN test_categories tc ON t.category_id = tc.id $where ORDER BY t.id DESC"); // Get categories for dropdown $categories = mysqli_query($conn, "SELECT * FROM test_categories WHERE status = 1"); // Get edit test data $edit_test = null; if(isset($_GET['edit'])) { $edit_id = $_GET['edit']; $edit_query = mysqli_query($conn, "SELECT * FROM tests WHERE id = $edit_id"); $edit_test = mysqli_fetch_assoc($edit_query); } // Messages $msg = isset($_GET['msg']) ? $_GET['msg'] : ''; ?>