Path : /home/vishqocm/pcib.in/debug/database/
File Upload :
Current File : /home/vishqocm/pcib.in/debug/database/create_theme_settings_table.php

<?php
/**
 * Create Theme Settings Table
 * 
 * This script creates the theme_settings table if it doesn't exist
 * and populates it with default theme settings.
 * 
 * Run this script if you encounter the error:
 * "Table 'popularcomputer.theme_settings' doesn't exist"
 */

// Include database configuration
require_once '../../admin/database/db_config.php';

// Display header
echo "<!DOCTYPE html>
<html>
<head>
    <title>Database Maintenance - Create Theme Settings Table</title>
    <style>
        body { font-family: Arial, sans-serif; line-height: 1.6; margin: 20px; color: #333; }
        h1 { color: #2c3e50; border-bottom: 2px solid #eee; padding-bottom: 10px; }
        h2 { color: #3498db; margin-top: 20px; }
        .container { max-width: 800px; margin: 0 auto; background: #f9f9f9; padding: 20px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        .success { color: #27ae60; background-color: #d5f5e3; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
        .error { color: #c0392b; background-color: #f8d7da; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
        .info { color: #2980b9; background-color: #d6eaf8; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
        .warning { color: #f39c12; background-color: #fef9e7; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
        table { width: 100%; border-collapse: collapse; margin: 20px 0; }
        table, th, td { border: 1px solid #ddd; }
        th, td { padding: 12px; text-align: left; }
        th { background-color: #f2f2f2; }
        a { color: #3498db; text-decoration: none; }
        a:hover { text-decoration: underline; }
        .btn { display: inline-block; background: #3498db; color: white; padding: 8px 16px; border-radius: 4px; text-decoration: none; margin-top: 15px; }
        .btn:hover { background: #2980b9; text-decoration: none; }
        pre { background: #f8f8f8; padding: 10px; border-radius: 4px; overflow-x: auto; }
        code { font-family: monospace; font-size: 14px; }
    </style>
</head>
<body>
    <div class='container'>
        <h1>Create Theme Settings Table</h1>
        <p>This script creates the theme_settings table if it doesn't exist and populates it with default values.</p>";

try {
    // Check if theme_settings table already exists
    $table_exists = $conn->query("SHOW TABLES LIKE 'theme_settings'");
    
    if ($table_exists->num_rows > 0) {
        echo "<div class='info'>Theme settings table already exists. No action taken.</div>";
    } else {
        // Create theme_settings table
        $create_table_sql = "CREATE TABLE theme_settings (
            id INT AUTO_INCREMENT PRIMARY KEY,
            setting_key VARCHAR(100) NOT NULL UNIQUE,
            setting_value TEXT NOT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        )";
        
        if ($conn->query($create_table_sql)) {
            echo "<div class='success'>Theme settings table created successfully.</div>";
            
            // Default theme settings to insert
            $default_settings = [
                ['theme_name', 'default'],
                ['header_bg_color', '#ffffff'],
                ['footer_bg_color', '#f8f9fc'],
                ['text_color', '#333333'],
                ['link_color', '#4e73df'],
                ['button_style', 'rounded'],
                ['font_family', 'Poppins, sans-serif'],
                ['custom_css', '']
            ];
            
            echo "<h2>Inserting Default Theme Settings</h2>";
            echo "<table>
                    <tr>
                        <th>Setting Key</th>
                        <th>Setting Value</th>
                        <th>Status</th>
                    </tr>";
            
            // Insert default settings
            $insert_stmt = $conn->prepare("INSERT INTO theme_settings (setting_key, setting_value) VALUES (?, ?)");
            $insert_stmt->bind_param("ss", $key, $value);
            
            foreach ($default_settings as $setting) {
                $key = $setting[0];
                $value = $setting[1];
                
                if ($insert_stmt->execute()) {
                    echo "<tr>
                            <td>{$key}</td>
                            <td>{$value}</td>
                            <td><span class='success'>Inserted</span></td>
                          </tr>";
                } else {
                    echo "<tr>
                            <td>{$key}</td>
                            <td>{$value}</td>
                            <td><span class='error'>Failed: {$insert_stmt->error}</span></td>
                          </tr>";
                }
            }
            
            echo "</table>";
            $insert_stmt->close();
            
            echo "<div class='success'>Default theme settings inserted successfully.</div>";
            
            // Show sample code for manual insertion
            echo "<h2>Theme Settings SQL</h2>";
            echo "<p>If you need to run this manually, here's the SQL code:</p>";
            
            echo "<pre><code>CREATE TABLE theme_settings (
    id INT AUTO_INCREMENT PRIMARY KEY,
    setting_key VARCHAR(100) NOT NULL UNIQUE,
    setting_value TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT INTO theme_settings (setting_key, setting_value) VALUES
('theme_name', 'default'),
('header_bg_color', '#ffffff'),
('footer_bg_color', '#f8f9fc'),
('text_color', '#333333'),
('link_color', '#4e73df'),
('button_style', 'rounded'),
('font_family', 'Poppins, sans-serif'),
('custom_css', '');</code></pre>";
        } else {
            throw new Exception("Failed to create theme_settings table: " . $conn->error);
        }
    }
    
    echo "<div class='links'>
            <a href='../../index.php' class='btn'>Return to Homepage</a>
            <a href='../../admin/index.php' class='btn'>Admin Dashboard</a>
            <a href='../index.php' class='btn'>Debug Tools</a>
          </div>";
    
} catch (Exception $e) {
    echo "<div class='error'>Error: " . $e->getMessage() . "</div>";
    echo "<div class='links'>
            <a href='../../index.php' class='btn'>Return to Homepage</a>
            <a href='../../admin/index.php' class='btn'>Admin Dashboard</a>
            <a href='../index.php' class='btn'>Debug Tools</a>
          </div>";
}

echo "</div></body></html>";
?>