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

<?php
/**
 * Fix Payments Date Column
 * 
 * This script fixes the payments table by adding the missing payment_date column.
 * 
 * Run this script if you encounter the error:
 * "Unknown column 'p.payment_date' in 'order clause'"
 */

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

// Display header
echo "<!DOCTYPE html>
<html>
<head>
    <title>Database Maintenance - Fix Payments Date Column</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>Fix Payments Date Column</h1>
        <p>This script checks and adds the missing payment_date column to the payments table.</p>";

try {
    // Check if payments table exists
    $table_exists = $conn->query("SHOW TABLES LIKE 'payments'");
    
    if ($table_exists->num_rows == 0) {
        echo "<div class='error'>Payments table does not exist. Please check your database setup.</div>";
    } else {
        echo "<div class='info'>Payments table exists. Checking columns...</div>";
        
        // Check if payment_date column exists
        $date_column_exists = $conn->query("SHOW COLUMNS FROM payments LIKE 'payment_date'");
        $has_date_column = ($date_column_exists && $date_column_exists->num_rows > 0);
        
        echo "<h2>Column Check Results:</h2>";
        echo "<table>
                <tr>
                    <th>Column Name</th>
                    <th>Status</th>
                </tr>
                <tr>
                    <td>payment_date</td>
                    <td>" . ($has_date_column ? "<span class='info'>Exists</span>" : "<span class='warning'>Missing</span>") . "</td>
                </tr>
              </table>";
        
        // Add payment_date column if it doesn't exist
        if (!$has_date_column) {
            if ($conn->query("ALTER TABLE payments ADD COLUMN payment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP")) {
                echo "<div class='success'>Successfully added 'payment_date' column to payments table.</div>";
                
                // Update all existing payments with the current date
                if ($conn->query("UPDATE payments SET payment_date = CURRENT_TIMESTAMP WHERE payment_date IS NULL")) {
                    echo "<div class='success'>Updated all existing payment records with current date.</div>";
                } else {
                    echo "<div class='warning'>Added column but couldn't update existing records: " . $conn->error . "</div>";
                }
            } else {
                echo "<div class='error'>Failed to add payment_date column: " . $conn->error . "</div>";
            }
        } else {
            echo "<div class='info'>The payment_date column already exists. No action needed.</div>";
        }
            
        // Display SQL for future reference
        echo "<h2>Useful SQL Commands:</h2>";
        echo "<p>Here are the SQL commands used to fix the payment_date column:</p>";
        
        echo "<pre><code>-- Add payment_date column if it doesn't exist
ALTER TABLE payments ADD COLUMN payment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

-- Update existing records with current date
UPDATE payments SET payment_date = CURRENT_TIMESTAMP WHERE payment_date IS NULL;</code></pre>";

        // Check if the issue is fixed
        $test_query = "SELECT p.*, CONCAT(u.first_name, ' ', u.last_name) as user_name 
                     FROM payments p 
                     JOIN users u ON p.user_id = u.id 
                     ORDER BY p.payment_date DESC LIMIT 1";
        
        // Try running the query that was causing the error
        if ($conn->query($test_query)) {
            echo "<div class='success'>✅ Test query ran successfully! The issue should be fixed.</div>";
        } else {
            echo "<div class='error'>❌ Test query still fails: " . $conn->error . " You may need additional fixes.</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>";
    
} 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>";
?>