# 🔧 AI Survey System - Database Setup Guide

## Problem Resolution: "Column not found: status"

**Issue**: `SQLSTATE[42S22]: Column not found: 1054 Unknown column 'status'`

**Cause**: The AI survey system requires specific database tables with proper column structure that weren't created yet.

## ✅ **Solution Applied**

### 1. **Database Setup Script Created**
- `setup_ai_survey_system.php` - Creates all required tables
- `diagnose_survey_system.php` - Verifies system integrity

### 2. **Tables Created**
```sql
✓ ai_surveys (status, title, description, survey_data...)
✓ hot_topics_tracker (status, topic_name, trend_score...)  
✓ survey_responses (survey_id, response_data...)
✓ survey_social_shares (survey_id, platform, status...)
✓ survey_analytics (survey_id, metric_name...)
✓ ai_survey_logs (event_type, status...)
```

### 3. **Sample Data Added**
- 5 Hot topics for survey generation
- 1 Sample active survey for testing
- Proper foreign key relationships

## 🚀 **Quick Fix Commands**

If you encounter database errors, run these:

```bash
# Setup all required tables
php setup_ai_survey_system.php

# Verify everything is working
php diagnose_survey_system.php
```

## 🎯 **System Components Now Working**

### ✅ **Survey Generator**
- http://localhost/HowToMalaysia/survey_generator.php
- Displays hot topics and recent surveys
- AI survey creation interface

### ✅ **Admin Dashboard** 
- http://localhost/HowToMalaysia/admin_dashboard.php
- Survey statistics and management
- AI article review system

### ✅ **Survey Management**
- http://localhost/HowToMalaysia/admin_surveys.php
- http://localhost/HowToMalaysia/admin_ai_survey_articles.php

## 📊 **Database Structure**

```
ai_surveys
├── id, title, description
├── status (draft/active/completed/archived)  ← Key column
├── target_responses, actual_responses
└── created_at, updated_at

hot_topics_tracker  
├── id, topic_name, description
├── status (active/inactive/used)  ← Key column
├── trend_score, source
└── created_at, updated_at
```

## ⚙️ **Prevention**

To avoid similar issues:
1. Always run `setup_ai_survey_system.php` on new installations
2. Use `diagnose_survey_system.php` to check system health
3. Backup database before major updates

## 🎉 **Status: RESOLVED**

- ✅ Database tables created with proper structure
- ✅ Sample data populated for testing
- ✅ Survey generator working correctly  
- ✅ Admin dashboard showing survey stats
- ✅ All survey-related features functional