HEX
Server: Apache
System: Linux 136-243-153-58.cprapid.com 4.18.0-553.81.1.el8_10.x86_64 #1 SMP Mon Oct 27 11:29:19 EDT 2025 x86_64
User: mytest (1001)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/mytest/.trash/migrate_users_table.php.5
<?php
/**
 * Migration Script: Add first_name, last_name, phone columns to users table
 * 
 * This script adds missing columns to the users table for existing databases.
 * For fresh installs, these columns are created automatically by Database.php
 * 
 * Run this ONCE on your production database via your database management tool.
 * 
 * IMPORTANT: This is for SQLite databases. Run these ALTER TABLE commands
 * in your database management tool (e.g., phpLiteAdmin, DB Browser for SQLite):
 * 
 * ALTER TABLE users ADD COLUMN first_name TEXT;
 * ALTER TABLE users ADD COLUMN last_name TEXT;
 * ALTER TABLE users ADD COLUMN phone TEXT;
 * 
 * Or run this PHP script once via browser: https://yourdomain.com/migrate_users_table.php
 */

require_once __DIR__ . '/config.php';
require_once __DIR__ . '/database/Database.php';

$lockFile = __DIR__ . '/data/.users_migration_done';
if (file_exists($lockFile)) {
    die('Migration already completed. Delete data/.users_migration_done to run again.');
}

try {
    $db = Database::getInstance()->getConnection();
    
    $result = $db->query("PRAGMA table_info(users)");
    $columns = [];
    while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
        $columns[] = $row['name'];
    }
    
    $columnsToAdd = [];
    if (!in_array('first_name', $columns)) {
        $columnsToAdd[] = 'first_name';
    }
    if (!in_array('last_name', $columns)) {
        $columnsToAdd[] = 'last_name';
    }
    if (!in_array('phone', $columns)) {
        $columnsToAdd[] = 'phone';
    }
    
    if (empty($columnsToAdd)) {
        echo "āœ… All columns already exist. No migration needed.\n";
        file_put_contents($lockFile, date('Y-m-d H:i:s'));
        exit;
    }
    
    foreach ($columnsToAdd as $column) {
        $db->exec("ALTER TABLE users ADD COLUMN $column TEXT");
        echo "āœ… Added column: $column\n";
    }
    
    file_put_contents($lockFile, date('Y-m-d H:i:s'));
    
    echo "\nāœ… Migration completed successfully!\n";
    echo "Added columns: " . implode(', ', $columnsToAdd) . "\n";
    echo "\nYou can now create and edit users with first name, last name, and phone fields.\n";
    
} catch (Exception $e) {
    echo "āŒ Migration failed: " . $e->getMessage() . "\n";
    exit(1);
}