File "check_locations.php"

Full path: /home/itsevak/public_html/prepaiddev.itsevak.com/operator/check_locations.php
File size: 6.78 B (6.78 KB bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php
require_once '../includes/functions.php';

// Require login and operator access
requireLogin();
if ($_SESSION['user_type'] !== 'operator') {
    redirect('/auth/login.php');
}

$user = getCurrentUser();
$db = Database::getInstance();

echo "<h2>Check From Locations Table</h2>";
echo "<p><strong>Operator:</strong> " . htmlspecialchars($user['name']) . " (ID: " . $user['id'] . ")</p>";

// Check if from_locations table exists
echo "<h3>1. Table Existence Check:</h3>";
try {
    $tables = $db->fetchAll("SHOW TABLES LIKE 'from_locations'");
    if (empty($tables)) {
        echo "<p style='color: red;'>❌ from_locations table does NOT exist!</p>";
        
        // Try to create the table
        if (isset($_POST['create_table'])) {
            echo "<p>Creating from_locations table...</p>";
            $createTable = "CREATE TABLE from_locations (
                id INT AUTO_INCREMENT PRIMARY KEY,
                name VARCHAR(255) NOT NULL,
                address TEXT NOT NULL,
                city VARCHAR(100) NOT NULL,
                state VARCHAR(100) NOT NULL,
                pincode VARCHAR(10),
                latitude DECIMAL(10, 8) NULL,
                longitude DECIMAL(11, 8) NULL,
                description TEXT,
                status ENUM('active', 'inactive') DEFAULT 'active',
                created_by INT NOT NULL,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                INDEX idx_name (name),
                INDEX idx_city (city),
                INDEX idx_status (status)
            )";
            
            $db->query($createTable);
            echo "<p style='color: green;'>✅ Table created successfully!</p>";
            
            // Insert a sample location
            $db->query(
                "INSERT INTO from_locations (name, address, city, state, pincode, description, created_by) VALUES (?, ?, ?, ?, ?, ?, ?)",
                ['Central Station', 'Main Street', 'Bangalore', 'Karnataka', '560001', 'Central transportation hub', 1]
            );
            echo "<p style='color: green;'>✅ Sample location added!</p>";
            
            echo "<script>location.reload();</script>";
        } else {
            echo "<form method='POST'>";
            echo "<button type='submit' name='create_table'>Create From Locations Table</button>";
            echo "</form>";
        }
    } else {
        echo "<p style='color: green;'>✅ from_locations table exists</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Error checking table: " . $e->getMessage() . "</p>";
}

// Check table structure
echo "<h3>2. Table Structure:</h3>";
try {
    $structure = $db->fetchAll("DESCRIBE from_locations");
    echo "<table border='1' style='border-collapse: collapse;'>";
    echo "<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>";
    foreach ($structure as $field) {
        echo "<tr>";
        echo "<td>" . $field['Field'] . "</td>";
        echo "<td>" . $field['Type'] . "</td>";
        echo "<td>" . $field['Null'] . "</td>";
        echo "<td>" . $field['Key'] . "</td>";
        echo "<td>" . ($field['Default'] ?? 'NULL') . "</td>";
        echo "<td>" . $field['Extra'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Error getting table structure: " . $e->getMessage() . "</p>";
}

// Check table data
echo "<h3>3. Table Data:</h3>";
try {
    $locations = $db->fetchAll("SELECT * FROM from_locations ORDER BY id");
    if (empty($locations)) {
        echo "<p style='color: orange;'>⚠️ No locations found in table</p>";
        
        // Add sample location if table is empty
        if (isset($_POST['add_sample'])) {
            echo "<p>Adding sample location...</p>";
            $db->query(
                "INSERT INTO from_locations (name, address, city, state, pincode, description, created_by) VALUES (?, ?, ?, ?, ?, ?, ?)",
                ['Central Station', 'Main Street', 'Bangalore', 'Karnataka', '560001', 'Central transportation hub', 1]
            );
            echo "<p style='color: green;'>✅ Sample location added!</p>";
            echo "<script>location.reload();</script>";
        } else {
            echo "<form method='POST'>";
            echo "<button type='submit' name='add_sample'>Add Sample Location</button>";
            echo "</form>";
        }
    } else {
        echo "<p style='color: green;'>✅ Found " . count($locations) . " location(s)</p>";
        echo "<table border='1' style='border-collapse: collapse;'>";
        echo "<tr><th>ID</th><th>Name</th><th>City</th><th>Status</th><th>Created</th></tr>";
        foreach ($locations as $location) {
            echo "<tr>";
            echo "<td>" . $location['id'] . "</td>";
            echo "<td>" . htmlspecialchars($location['name']) . "</td>";
            echo "<td>" . htmlspecialchars($location['city']) . "</td>";
            echo "<td>" . $location['status'] . "</td>";
            echo "<td>" . $location['created_at'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Error checking table data: " . $e->getMessage() . "</p>";
}

// Check active locations
echo "<h3>4. Active Locations:</h3>";
try {
    $activeLocations = $db->fetchAll("SELECT * FROM from_locations WHERE status = 'active' ORDER BY name");
    if (empty($activeLocations)) {
        echo "<p style='color: red;'>❌ No active locations found!</p>";
        
        // Activate all locations
        if (isset($_POST['activate_all'])) {
            echo "<p>Activating all locations...</p>";
            $db->query("UPDATE from_locations SET status = 'active'");
            echo "<p style='color: green;'>✅ All locations activated!</p>";
            echo "<script>location.reload();</script>";
        } else {
            echo "<form method='POST'>";
            echo "<button type='submit' name='activate_all'>Activate All Locations</button>";
            echo "</form>";
        }
    } else {
        echo "<p style='color: green;'>✅ Found " . count($activeLocations) . " active location(s)</p>";
        echo "<ul>";
        foreach ($activeLocations as $location) {
            echo "<li><strong>" . htmlspecialchars($location['name']) . "</strong> - " . htmlspecialchars($location['city']) . "</li>";
        }
        echo "</ul>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Error checking active locations: " . $e->getMessage() . "</p>";
}

echo "<hr>";
echo "<p><a href='index.php'>Back to Operator Dashboard</a></p>";

?>