Skip to content Skip to footer
prefix . ‘csm_clients’; $sessions_table = $wpdb->prefix . ‘csm_sessions’; ?> CSM Emergency Fix

🔧 Client Session Manager – Emergency Diagnostic & Fix

📊 Step 1: Database Diagnosis“; // Check if tables exist $tables_exist = true; $clients_exists = $wpdb->get_var(“SHOW TABLES LIKE ‘$clients_table'”) === $clients_table; $sessions_exists = $wpdb->get_var(“SHOW TABLES LIKE ‘$sessions_table'”) === $sessions_table; if (!$clients_exists || !$sessions_exists) { echo ‘
CRITICAL: Plugin tables do not exist! The plugin may not be activated properly.
‘; echo ‘
‘; $tables_exist = false; } else { echo ‘
✅ Plugin tables exist
‘; } if ($tables_exist) { // Check table structure echo “

Table Structure Check

“; $columns = $wpdb->get_results(“DESCRIBE $clients_table”); $column_names = array_column($columns, ‘Field’); echo ““; echo ““; $required_columns = [‘id’, ‘first_name’, ‘surname’, ’email’, ‘telephone’, ‘total_sessions’, ‘remaining_sessions’, ‘created_at’]; $old_column = ‘name’; $has_first_name = in_array(‘first_name’, $column_names); $has_surname = in_array(‘surname’, $column_names); $has_old_name = in_array(‘name’, $column_names); $has_email = in_array(’email’, $column_names); foreach ($columns as $col) { $status = in_array($col->Field, $required_columns) ? ‘✅’ : ($col->Field === ‘name’ ? ‘⚠️ OLD’ : ”); echo ““; } echo “
Column NameTypeStatus
{$col->Field}{$col->Type}{$status}
“; // Diagnosis if ($has_old_name && !$has_first_name) { echo ‘
PROBLEM FOUND: Database still has OLD structure (name field). Migration was not run!
‘; echo ‘
‘; echo ‘‘; echo ‘
‘; } elseif (!$has_first_name || !$has_surname) { echo ‘
PROBLEM FOUND: Missing first_name or surname columns!
‘; echo ‘
‘; echo ‘‘; echo ‘
‘; } else { echo ‘
✅ Database structure is correct (v2.1)
‘; } // Check for email unique constraint $indexes = $wpdb->get_results(“SHOW INDEXES FROM $clients_table WHERE Column_name = ’email'”); $has_unique_email = false; foreach ($indexes as $index) { if ($index->Non_unique == 0) { $has_unique_email = true; break; } } if ($has_unique_email) { echo ‘
✅ Email has UNIQUE constraint
‘; } else { echo ‘
⚠️ Email does NOT have UNIQUE constraint
‘; } // Count records echo “

Data Count

“; $client_count = $wpdb->get_var(“SELECT COUNT(*) FROM $clients_table”); $session_count = $wpdb->get_var(“SELECT COUNT(*) FROM $sessions_table”); echo ““; echo ““; echo ““; echo ““; echo “
TableRecord Count
Clients$client_count
Sessions$session_count
“; if ($client_count == 0) { echo ‘
⚠️ No clients found! They may have been lost during migration.
‘; } // Show sample data if ($client_count > 0) { echo “

Sample Client Data (First 5)

“; $sample_clients = $wpdb->get_results(“SELECT * FROM $clients_table LIMIT 5”); if (!empty($sample_clients)) { echo ““; echo ““; foreach ($sample_clients[0] as $key => $value) { echo ““; } echo ““; foreach ($sample_clients as $client) { echo ““; foreach ($client as $value) { echo ““; } echo ““; } echo “
” . esc_html($key) . “
” . esc_html($value) . “
“; } } } ?>

🔧 Step 2: Available Fixes

📝 Manual Migration SQL

If the automatic migration fails, run this SQL in phpMyAdmin:

⚠️ Important Notes

  • Always backup your database before running fixes!
  • If you had clients before the update, they should still be in the database
  • The migration script converts old “name” field to “first_name” and “surname”
  • DELETE this file after use for security!
Starting migration…
‘; // Check if old structure exists $columns = $wpdb->get_results(“DESCRIBE $clients_table”); $column_names = array_column($columns, ‘Field’); $has_old_name = in_array(‘name’, $column_names); if (!$has_old_name) { echo ‘
❌ Old “name” column not found. Nothing to migrate.
‘; exit; } // Step 1: Add new columns $wpdb->query(“ALTER TABLE `{$clients_table}` ADD COLUMN IF NOT EXISTS `first_name` VARCHAR(100) NOT NULL DEFAULT ” AFTER `id`”); $wpdb->query(“ALTER TABLE `{$clients_table}` ADD COLUMN IF NOT EXISTS `surname` VARCHAR(100) NOT NULL DEFAULT ” AFTER `first_name`”); echo ‘
✅ Added first_name and surname columns
‘; // Step 2: Migrate data $result = $wpdb->query(” UPDATE `{$clients_table}` SET `first_name` = SUBSTRING_INDEX(`name`, ‘ ‘, 1), `surname` = SUBSTRING_INDEX(`name`, ‘ ‘, -1) WHERE `name` IS NOT NULL AND `name` != ” “); echo ‘
✅ Migrated ‘ . $result . ‘ client names
‘; // Step 3: Handle single-word names $wpdb->query(“UPDATE `{$clients_table}` SET `surname` = `first_name` WHERE `surname` = ” OR `surname` IS NULL”); echo ‘
✅ Fixed single-word names
‘; // Step 4: Drop old column $wpdb->query(“ALTER TABLE `{$clients_table}` DROP COLUMN `name`”); echo ‘
✅ Removed old “name” column
‘; // Step 5: Try to add unique constraint (may fail if duplicates exist) $result = $wpdb->query(“ALTER TABLE `{$clients_table}` ADD UNIQUE KEY `email` (`email`)”); if ($result !== false) { echo ‘
✅ Added UNIQUE constraint to email
‘; } else { echo ‘
⚠️ Could not add UNIQUE constraint (you may have duplicate emails)
‘; } echo ‘
✅ Migration completed! Page will reload…
‘; exit; } if ($action === ‘add_test’) { $result = $wpdb->insert( $clients_table, array( ‘first_name’ => ‘Test’, ‘surname’ => ‘Client’, ’email’ => ‘test@example.com’, ‘telephone’ => ‘555-1234’, ‘total_sessions’ => 5, ‘remaining_sessions’ => 5 ) ); if ($result) { echo ‘
✅ Test client added successfully!
‘; } else { echo ‘
❌ Failed to add test client: ‘ . $wpdb->last_error . ‘
‘; } exit; } if ($action === ‘debug’) { echo “

Debug Information

“; echo “
";
        echo "WordPress Version: " . get_bloginfo('version') . "\n";
        echo "PHP Version: " . PHP_VERSION . "\n";
        echo "Table Prefix: " . $wpdb->prefix . "\n";
        echo "\nClients Table: " . $clients_table . "\n";
        echo "Sessions Table: " . $sessions_table . "\n";
        echo "\nLast Database Error: " . $wpdb->last_error . "\n";
        echo "\nClients Table Structure:\n";
        print_r($wpdb->get_results("DESCRIBE $clients_table"));
        echo "
“; exit; } if ($action === ‘reset’) { $wpdb->query(“TRUNCATE TABLE `{$sessions_table}`”); $wpdb->query(“TRUNCATE TABLE `{$clients_table}`”); // Recreate tables with correct structure $charset_collate = $wpdb->get_charset_collate(); $wpdb->query(“DROP TABLE IF EXISTS `{$clients_table}`”); $wpdb->query(“CREATE TABLE `{$clients_table}` ( id mediumint(9) NOT NULL AUTO_INCREMENT, first_name varchar(100) NOT NULL, surname varchar(100) NOT NULL, email varchar(200) NOT NULL, telephone varchar(50), total_sessions int(11) NOT NULL DEFAULT 0, remaining_sessions int(11) NOT NULL DEFAULT 0, created_at datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY email (email) ) $charset_collate”); echo ‘
✅ Database reset completed with v2.1 structure!
‘; exit; } } ?>
Looking for a PT?