getConnection(); $action = sanitizeInput($_POST['action']); switch ($action) { case 'get_user': $user_id = intval($_POST['user_id']); $stmt = $db->prepare("SELECT * FROM users WHERE id = ?"); $stmt->execute([$user_id]); $user = $stmt->fetch(); if ($user) { unset($user['password']); jsonResponse(true, 'User found', $user); } else { jsonResponse(false, 'User not found'); } break; case 'add_user': try { $name = sanitizeInput($_POST['name']); $email = sanitizeInput($_POST['email'] ?? ''); $mobile = sanitizeInput($_POST['mobile']); $gender = sanitizeInput($_POST['gender']); $dob = sanitizeInput($_POST['dob']); $user_type = sanitizeInput($_POST['user_type']); $status = sanitizeInput($_POST['status'] ?? 'approved'); $membership_type = sanitizeInput($_POST['membership_type']); $state = sanitizeInput($_POST['state']); $district = sanitizeInput($_POST['district']); $pincode = sanitizeInput($_POST['pincode']); $address = sanitizeInput($_POST['address']); // Generate temporary password $tempPassword = 'temp' . rand(1000, 9999); $hashedPassword = password_hash($tempPassword, PASSWORD_DEFAULT); // Generate registration ID $registration_id = 'SDJVP' . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); // Handle profile image upload $profile_image = null; if (isset($_FILES['profile_image']) && $_FILES['profile_image']['error'] === UPLOAD_ERR_OK) { $profile_image = uploadImage($_FILES['profile_image']); } $stmt = $db->prepare("INSERT INTO users (name, email, password, mobile, gender, dob, sdw_type, sdw_name, aadhar, state, district, address, pincode, membership_type, profile_image, user_type, status, registration_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([ $name, $email, $hashedPassword, $mobile, $gender, $dob, 'S/O', 'Father Name', '000000000000', $state, $district, $address, $pincode, $membership_type, $profile_image, $user_type, $status, $registration_id ]); jsonResponse(true, "User added successfully. Registration ID: $registration_id, Temporary password: $tempPassword"); } catch (Exception $e) { jsonResponse(false, 'Failed to add user: ' . $e->getMessage()); } break; case 'update_user': try { $user_id = intval($_POST['user_id']); $name = sanitizeInput($_POST['name']); $email = sanitizeInput($_POST['email'] ?? ''); $mobile = sanitizeInput($_POST['mobile']); $gender = sanitizeInput($_POST['gender']); $dob = sanitizeInput($_POST['dob']); $user_type = sanitizeInput($_POST['user_type']); $status = sanitizeInput($_POST['status']); $membership_type = sanitizeInput($_POST['membership_type']); $state = sanitizeInput($_POST['state']); $district = sanitizeInput($_POST['district']); $pincode = sanitizeInput($_POST['pincode']); $address = sanitizeInput($_POST['address']); $updateFields = "name = ?, email = ?, mobile = ?, gender = ?, dob = ?, user_type = ?, status = ?, membership_type = ?, state = ?, district = ?, pincode = ?, address = ?"; $params = [$name, $email, $mobile, $gender, $dob, $user_type, $status, $membership_type, $state, $district, $pincode, $address]; // Handle profile image upload if (isset($_FILES['profile_image']) && $_FILES['profile_image']['error'] === UPLOAD_ERR_OK) { // Delete old image $stmt = $db->prepare("SELECT profile_image FROM users WHERE id = ?"); $stmt->execute([$user_id]); $oldImage = $stmt->fetchColumn(); if ($oldImage && file_exists('uploads/' . $oldImage)) { unlink('uploads/' . $oldImage); } $profile_image = uploadImage($_FILES['profile_image']); $updateFields .= ", profile_image = ?"; $params[] = $profile_image; } $params[] = $user_id; $stmt = $db->prepare("UPDATE users SET $updateFields WHERE id = ?"); $stmt->execute($params); jsonResponse(true, 'User updated successfully'); } catch (Exception $e) { jsonResponse(false, 'Failed to update user: ' . $e->getMessage()); } break; case 'delete_user': try { $user_id = intval($_POST['user_id']); // Delete profile image if exists $stmt = $db->prepare("SELECT profile_image FROM users WHERE id = ?"); $stmt->execute([$user_id]); $profile_image = $stmt->fetchColumn(); if ($profile_image && file_exists('uploads/' . $profile_image)) { unlink('uploads/' . $profile_image); } $stmt = $db->prepare("DELETE FROM users WHERE id = ?"); $stmt->execute([$user_id]); jsonResponse(true, 'User deleted successfully'); } catch (Exception $e) { jsonResponse(false, 'Failed to delete user: ' . $e->getMessage()); } break; case 'bulk_action': try { $user_ids = json_decode($_POST['user_ids'], true); $bulk_action = sanitizeInput($_POST['bulk_action']); if (!is_array($user_ids) || empty($user_ids)) { jsonResponse(false, 'No users selected'); } $placeholders = str_repeat('?,', count($user_ids) - 1) . '?'; switch ($bulk_action) { case 'approve': $stmt = $db->prepare("UPDATE users SET status = 'approved' WHERE id IN ($placeholders)"); $stmt->execute($user_ids); jsonResponse(true, count($user_ids) . ' users approved successfully'); break; case 'reject': $stmt = $db->prepare("UPDATE users SET status = 'rejected' WHERE id IN ($placeholders)"); $stmt->execute($user_ids); jsonResponse(true, count($user_ids) . ' users rejected successfully'); break; case 'delete': // Delete profile images $stmt = $db->prepare("SELECT profile_image FROM users WHERE id IN ($placeholders) AND profile_image IS NOT NULL"); $stmt->execute($user_ids); $images = $stmt->fetchAll(PDO::FETCH_COLUMN); foreach ($images as $image) { if (file_exists('uploads/' . $image)) { unlink('uploads/' . $image); } } // Delete users $stmt = $db->prepare("DELETE FROM users WHERE id IN ($placeholders)"); $stmt->execute($user_ids); jsonResponse(true, count($user_ids) . ' users deleted successfully'); break; case 'change_type': $user_type = sanitizeInput($_POST['user_type']); $params = array_merge([$user_type], $user_ids); $stmt = $db->prepare("UPDATE users SET user_type = ? WHERE id IN ($placeholders)"); $stmt->execute($params); jsonResponse(true, count($user_ids) . " users changed to $user_type successfully"); break; } } catch (Exception $e) { jsonResponse(false, 'Bulk action failed: ' . $e->getMessage()); } break; case 'export_users': try { $stmt = $db->prepare("SELECT id, name, email, mobile, gender, dob, user_type, status, membership_type, state, district, pincode, address, registration_id, created_at FROM users ORDER BY created_at DESC"); $stmt->execute(); $data = $stmt->fetchAll(); header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="users_export_' . date('Y-m-d') . '.csv"'); $output = fopen('php://output', 'w'); fputcsv($output, ['ID', 'Name', 'Email', 'Mobile', 'Gender', 'DOB', 'User Type', 'Status', 'Membership', 'State', 'District', 'Pincode', 'Address', 'Registration ID', 'Created At']); foreach ($data as $row) { fputcsv($output, $row); } fclose($output); exit; } catch (Exception $e) { jsonResponse(false, 'Export failed: ' . $e->getMessage()); } break; } } // Get all users with pagination and filters $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $limit = 20; $offset = ($page - 1) * $limit; $search = sanitizeInput($_GET['search'] ?? ''); $status = sanitizeInput($_GET['status'] ?? ''); $user_type = sanitizeInput($_GET['user_type'] ?? ''); $membership = sanitizeInput($_GET['membership'] ?? ''); $db = Database::getInstance()->getConnection(); // Build query $sql = "SELECT * FROM users WHERE 1=1"; $params = []; if ($search) { $sql .= " AND (name LIKE ? OR email LIKE ? OR mobile LIKE ? OR registration_id LIKE ?)"; $params[] = "%$search%"; $params[] = "%$search%"; $params[] = "%$search%"; $params[] = "%$search%"; } if ($status) { $sql .= " AND status = ?"; $params[] = $status; } if ($user_type) { $sql .= " AND user_type = ?"; $params[] = $user_type; } if ($membership) { $sql .= " AND membership_type = ?"; $params[] = $membership; } // Get total count $count_sql = str_replace("SELECT *", "SELECT COUNT(*)", $sql); $stmt = $db->prepare($count_sql); $stmt->execute($params); $total_records = $stmt->fetchColumn(); $total_pages = ceil($total_records / $limit); // Get users with pagination $sql .= " ORDER BY created_at DESC LIMIT ? OFFSET ?"; $params[] = $limit; $params[] = $offset; $stmt = $db->prepare($sql); $stmt->execute($params); $users = $stmt->fetchAll(); ?>
Complete CRUD operations for all users ( total)
| ID | Name | Mobile | Membership | Status | Type | Joined | Actions | ||
|---|---|---|---|---|---|---|---|---|---|
|
|
|