================================================================================
UPDATE PHP FILES FOR RENAMED TABLES (OPTION B)
================================================================================

If you chose OPTION B (independent tables with auth_ prefix), you need to
update the PHP files to reference the new table names.

================================================================================
WHAT CHANGED
================================================================================

Old Table Names → New Table Names:
  users           → auth_users
  user_sessions   → auth_sessions
  activity_log    → auth_activity_log
  login_attempts  → auth_login_attempts

Your existing tables (users, user_sessions, activity_log) remain untouched
for your file upload system.

================================================================================
FILES THAT NEED UPDATING
================================================================================

You need to update table names in these PHP files:

1. includes/auth.php - Core authentication
2. forms/user_management.php - User management
3. forms/profile.php - User profile
4. forms/activity_log.php - Activity viewer

================================================================================
METHOD 1: FIND AND REPLACE (EASIEST)
================================================================================

Open each PHP file and use Find & Replace:

Find: 'users'
Replace with: 'auth_users'

Find: 'user_sessions'
Replace with: 'auth_sessions'

Find: 'activity_log'
Replace with: 'auth_activity_log'

Find: 'login_attempts'
Replace with: 'auth_login_attempts'

CAREFUL: Make sure you're replacing SQL table names, not function names!

================================================================================
METHOD 2: SPECIFIC CHANGES BY FILE
================================================================================

FILE: includes/auth.php
---------------------
Line ~75: SELECT * FROM users WHERE → SELECT * FROM auth_users WHERE
Line ~110: UPDATE users SET → UPDATE auth_users SET
Line ~135: INSERT INTO user_sessions → INSERT INTO auth_sessions
Line ~150: UPDATE users SET remember_token → UPDATE auth_users SET remember_token
Line ~200: DELETE FROM user_sessions → DELETE FROM auth_sessions
Line ~250: INSERT INTO login_attempts → INSERT INTO auth_login_attempts
Line ~280: INSERT INTO activity_log → INSERT INTO auth_activity_log
Line ~300: DELETE FROM user_sessions WHERE → DELETE FROM auth_sessions WHERE
Line ~330: SELECT * FROM users WHERE → SELECT * FROM auth_users WHERE

FILE: forms/user_management.php
-------------------------------
Line ~20: SELECT * FROM users WHERE → SELECT * FROM auth_users WHERE
Line ~50: UPDATE users SET → UPDATE auth_users SET
Line ~75: UPDATE users SET password_hash → UPDATE auth_users SET password_hash
Line ~90: SELECT * FROM users WHERE → SELECT * FROM auth_users WHERE
Line ~100: INSERT INTO users → INSERT INTO auth_users
Line ~130: SELECT u.*, s.first_name → SELECT u.*, s.first_name FROM auth_users u

FILE: forms/change_password.php
-------------------------------
Line ~25: SELECT password_hash FROM users → SELECT password_hash FROM auth_users
Line ~35: UPDATE users SET password_hash → UPDATE auth_users SET password_hash

FILE: forms/profile.php
-----------------------
Line ~20: FROM users u → FROM auth_users u
Line ~40: UPDATE users SET → UPDATE auth_users SET
Line ~70: FROM activity_log WHERE → FROM auth_activity_log WHERE

FILE: forms/activity_log.php
----------------------------
Line ~70: FROM activity_log al → FROM auth_activity_log al
Line ~75: LEFT JOIN users u → LEFT JOIN auth_users u
Line ~90: SELECT DISTINCT action FROM activity_log → SELECT DISTINCT action FROM auth_activity_log
Line ~95: SELECT DISTINCT entity_type FROM activity_log → SELECT DISTINCT entity_type FROM auth_activity_log

================================================================================
METHOD 3: USE CONFIG CONSTANT (RECOMMENDED)
================================================================================

Create a configuration constant for table names.

In includes/config.php, ADD:

// Authentication table names (change if using renamed tables)
define('AUTH_USERS_TABLE', 'auth_users');
define('AUTH_SESSIONS_TABLE', 'auth_sessions');
define('AUTH_ACTIVITY_TABLE', 'auth_activity_log');
define('AUTH_ATTEMPTS_TABLE', 'auth_login_attempts');

Then in each PHP file, replace table names with constants:

FROM users → FROM " . AUTH_USERS_TABLE . "
FROM user_sessions → FROM " . AUTH_SESSIONS_TABLE . "
FROM activity_log → FROM " . AUTH_ACTIVITY_TABLE . "
FROM login_attempts → FROM " . AUTH_ATTEMPTS_TABLE . "

Example:
  OLD: SELECT * FROM users WHERE
  NEW: SELECT * FROM " . AUTH_USERS_TABLE . " WHERE

This way, you can switch back to original names by just changing config.php!

================================================================================
VERIFICATION CHECKLIST
================================================================================

After updating PHP files, test:

□ Can login at login.php
□ Dashboard loads after login
□ User menu appears (top right)
□ Can access user management (Admin)
□ Can change password
□ Can view profile
□ Can view activity log
□ Logout works
□ Cannot access pages without login

If any fail, check that table names were updated correctly in that feature.

================================================================================
QUICK TEST QUERIES
================================================================================

Run these in phpMyAdmin to verify tables exist:

SELECT * FROM auth_users;
SELECT * FROM auth_sessions;
SELECT * FROM auth_activity_log LIMIT 10;
SELECT * FROM auth_login_attempts LIMIT 10;

All should return data or empty result (not error).

================================================================================
ROLLBACK IF NEEDED
================================================================================

If you need to switch back to original table names:

OPTION 1: Rename tables back
  RENAME TABLE auth_users TO users_auth;
  RENAME TABLE auth_sessions TO sessions_auth;
  RENAME TABLE auth_activity_log TO activity_auth;

  Then use OPTION A script to merge with existing.

OPTION 2: Drop new tables and use OPTION A
  DROP TABLE auth_users;
  DROP TABLE auth_sessions;
  DROP TABLE auth_activity_log;
  DROP TABLE auth_login_attempts;

  Then run OPTION_A_merge_existing_tables.sql

================================================================================
SUMMARY: WHICH FILES TO UPDATE
================================================================================

If you chose OPTION B (renamed tables), update these 4 files:

✓ includes/auth.php (~15 table name references)
✓ forms/user_management.php (~10 table name references)
✓ forms/change_password.php (~2 table name references)
✓ forms/profile.php (~5 table name references)
✓ forms/activity_log.php (~8 table name references)

METHOD: Find & Replace in each file
  users → auth_users
  user_sessions → auth_sessions
  activity_log → auth_activity_log
  login_attempts → auth_login_attempts

OR: Add constants to config.php and use them in queries

================================================================================
NEED HELP?
================================================================================

If updates fail, check:
1. Did you replace ALL instances?
2. Did you only replace table names (not function names)?
3. Are quotes correct in SQL queries?
4. Did you test each feature after updating?

Common mistake: Replacing "users" in function names like "get_users()"
Fix: Only replace in SQL queries like "FROM users" or "INSERT INTO users"

================================================================================
