Path : /home/vishqocm/pcib.in/admin/
File Upload :
Current File : //home/vishqocm/pcib.in/admin/check_db_tables.php

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

// Enable error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

echo '<html><head><title>Database Check</title>';
echo '<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1, h2, h3 { color: #333; }
table { border-collapse: collapse; width: 100%; margin-bottom: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
tr:nth-child(even) { background-color: #f9f9f9; }
.success { color: green; background-color: #dff0d8; padding: 10px; border-radius: 5px; margin: 10px 0; }
.error { color: red; background-color: #f2dede; padding: 10px; border-radius: 5px; margin: 10px 0; }
.info { color: #31708f; background-color: #d9edf7; padding: 10px; border-radius: 5px; margin: 10px 0; }
</style>';
echo '</head><body>';

echo "<h1>Database Table Check</h1>";

// Check if course_reviews table exists
$table_exists = $conn->query("SHOW TABLES LIKE 'course_reviews'");
if ($table_exists && $table_exists->num_rows > 0) {
    echo "<div class='success'>course_reviews table exists.</div>";
    
    // Show structure
    echo "<h2>course_reviews Table Structure</h2>";
    $result = $conn->query("SHOW COLUMNS FROM course_reviews");
    if ($result) {
        echo "<table><tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>";
        while ($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>" . $row['Field'] . "</td>";
            echo "<td>" . $row['Type'] . "</td>";
            echo "<td>" . $row['Null'] . "</td>";
            echo "<td>" . $row['Key'] . "</td>";
            echo "<td>" . ($row['Default'] !== NULL ? $row['Default'] : "NULL") . "</td>";
            echo "<td>" . $row['Extra'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
    // Check if there are any reviews
    $review_count = $conn->query("SELECT COUNT(*) as count FROM course_reviews");
    if ($review_count) {
        $count_data = $review_count->fetch_assoc();
        echo "<div class='info'>Total reviews in database: " . $count_data['count'] . "</div>";
        
        if ($count_data['count'] > 0) {
            // Show some sample reviews
            echo "<h2>Sample Reviews</h2>";
            $sample_reviews = $conn->query("SELECT cr.*, c.title as course_title, u.first_name, u.last_name 
                                         FROM course_reviews cr 
                                         LEFT JOIN courses c ON cr.course_id = c.id 
                                         LEFT JOIN users u ON cr.user_id = u.id 
                                         LIMIT 10");
            
            if ($sample_reviews && $sample_reviews->num_rows > 0) {
                echo "<table><tr><th>ID</th><th>Course</th><th>User</th><th>Rating</th><th>Comment</th><th>Date</th></tr>";
                while ($review = $sample_reviews->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>" . $review['id'] . "</td>";
                    echo "<td>" . ($review['course_title'] ?? 'Unknown Course') . "</td>";
                    echo "<td>" . ($review['first_name'] . ' ' . $review['last_name'] ?? 'Unknown User') . "</td>";
                    echo "<td>" . $review['rating'] . "</td>";
                    echo "<td>" . $review['comment'] . "</td>";
                    echo "<td>" . $review['created_at'] . "</td>";
                    echo "</tr>";
                }
                echo "</table>";
            } else {
                echo "<div class='error'>Error fetching sample reviews: " . $conn->error . "</div>";
            }
        } else {
            echo "<div class='info'>No course reviews found in the database.</div>";
            
            // Create a sample review for testing
            echo "<h2>Create Sample Review</h2>";
            echo "<form method='post' action=''>";
            echo "<select name='course_id' required>";
            
            $courses = $conn->query("SELECT id, title FROM courses LIMIT 10");
            if ($courses && $courses->num_rows > 0) {
                while ($course = $courses->fetch_assoc()) {
                    echo "<option value='" . $course['id'] . "'>" . $course['title'] . "</option>";
                }
            } else {
                echo "<option value=''>No courses available</option>";
            }
            
            echo "</select>";
            
            // User selection
            echo "<select name='user_id' required>";
            
            $users = $conn->query("SELECT id, first_name, last_name FROM users LIMIT 10");
            if ($users && $users->num_rows > 0) {
                while ($user = $users->fetch_assoc()) {
                    echo "<option value='" . $user['id'] . "'>" . $user['first_name'] . " " . $user['last_name'] . "</option>";
                }
            } else {
                echo "<option value=''>No users available</option>";
            }
            
            echo "</select>";
            
            echo "<select name='rating' required>";
            for ($i = 1; $i <= 5; $i++) {
                echo "<option value='" . $i . "'>" . $i . " stars</option>";
            }
            echo "</select>";
            
            echo "<textarea name='comment' placeholder='Review comment'></textarea>";
            echo "<button type='submit' name='add_review'>Add Sample Review</button>";
            echo "</form>";
            
            // Process review submission
            if (isset($_POST['add_review'])) {
                $course_id = $_POST['course_id'];
                $user_id = $_POST['user_id'];
                $rating = $_POST['rating'];
                $comment = $_POST['comment'];
                
                $insert_result = $conn->query("INSERT INTO course_reviews (course_id, user_id, rating, comment, created_at) 
                                        VALUES ('$course_id', '$user_id', '$rating', '$comment', NOW())");
                
                if ($insert_result) {
                    echo "<div class='success'>Sample review added successfully!</div>";
                } else {
                    echo "<div class='error'>Error adding sample review: " . $conn->error . "</div>";
                }
            }
        }
    } else {
        echo "<div class='error'>Error checking review count: " . $conn->error . "</div>";
    }
} else {
    echo "<div class='error'>course_reviews table does not exist!</div>";
    
    // Offer to create the table
    echo "<h2>Create course_reviews Table</h2>";
    echo "<div class='info'>Would you like to create the course_reviews table?</div>";
    
    echo "<form method='post' action=''>";
    echo "<button type='submit' name='create_table'>Create Table</button>";
    echo "</form>";
    
    // Process table creation
    if (isset($_POST['create_table'])) {
        $create_table_sql = "CREATE TABLE course_reviews (
            id INT(11) NOT NULL AUTO_INCREMENT,
            course_id INT(11) NOT NULL,
            user_id INT(11) NOT NULL,
            rating INT(1) NOT NULL,
            comment TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY idx_course_id (course_id),
            KEY idx_user_id (user_id)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        
        $create_result = $conn->query($create_table_sql);
        
        if ($create_result) {
            echo "<div class='success'>course_reviews table created successfully!</div>";
            echo "<div class='info'>Refresh this page to see the table structure and add sample reviews.</div>";
        } else {
            echo "<div class='error'>Error creating course_reviews table: " . $conn->error . "</div>";
        }
    }
}

echo "<h2>Next Steps</h2>";
echo "<p>Return to <a href='../index.php'>Homepage</a> or <a href='../admin/index.php'>Admin Dashboard</a>.</p>";
echo "</body></html>";
?>