# phpMyAdmin Step-by-Step Guide

## Fix Expenses Table ID Column

### Step 1: Select the Correct Database
1. Open phpMyAdmin in cPanel
2. Click on database: **acbptxvs_branch_system** (in the left sidebar)
3. Make sure you see the database name at the top

### Step 2: Check Current Table Structure
Click on the **SQL** tab and run:
```sql
DESCRIBE expenses;
```
Look at the `id` column - note its type (should be bigint or varchar)

### Step 3: Check How Many Records Exist
```sql
SELECT COUNT(*) as total_expenses FROM expenses;
```
This shows how many expense records will be deleted (they'll be backed up first)

### Step 4: Create Backup
Run these queries **ONE AT A TIME**:

First query:
```sql
DROP TABLE IF EXISTS expenses_backup;
```

Second query:
```sql
CREATE TABLE expenses_backup AS SELECT * FROM expenses;
```

Third query (verify backup):
```sql
SELECT COUNT(*) as backup_count FROM expenses_backup;
```
Make sure this shows the same number as Step 3!

### Step 5: Truncate the Table
⚠️ **WARNING**: This deletes all expense records (but they're in expenses_backup)
```sql
TRUNCATE TABLE expenses;
```

### Step 6: Fix the ID Column
```sql
ALTER TABLE expenses 
MODIFY COLUMN id bigint NOT NULL AUTO_INCREMENT;
```

### Step 7: Verify the Fix
```sql
DESCRIBE expenses;
```
Check that `id` column shows:
- Type: `bigint`
- Extra: `auto_increment`

### Step 8: Verify Table is Empty
```sql
SELECT COUNT(*) as current_expenses FROM expenses;
```
Should show 0 records

### Step 9: Check Backup Still Has Data
```sql
SELECT COUNT(*) as backup_expenses FROM expenses_backup;
```
Should show your original number of records

---

## After Database Fix

### Upload Updated Files
Upload these 3 files to your server (via cPanel File Manager or FTP):
1. `expenses/models.py`
2. `expenses/forms.py`
3. `expenses/views.py`

### Restart Application
In cPanel:
1. Go to **Setup Python App**
2. Find your application
3. Click **Restart** button

### Test the Fix
1. Login to your application
2. Go to **Expenses** > **Add Expense**
3. Check:
   - ✓ Branch dropdown shows all your accessible branches (not just "main branch")
   - ✓ You can fill out the form
   - ✓ Click Save
   - ✓ No error about "Incorrect integer value"
   - ✓ Expense is created successfully

---

## Troubleshooting

### Error: "Access denied to database 'information_schema'"
**Solution**: Make sure you selected database `acbptxvs_branch_system` in the left sidebar before running queries

### Error: "Table 'expenses_backup' already exists"
**Solution**: Run `DROP TABLE IF EXISTS expenses_backup;` first

### Error: "Cannot truncate a table referenced in a foreign key constraint"
**Solution**: Run these instead:
```sql
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE expenses;
SET FOREIGN_KEY_CHECKS = 1;
```

### Want to Restore Old Data?
If you need to restore the backed up expenses:
```sql
INSERT INTO expenses SELECT * FROM expenses_backup;
```
Note: This will only work if you haven't uploaded the new model files yet!

---

## Quick Copy-Paste Version

If you're confident, copy and paste this entire block (but run line by line to check for errors):

```sql
-- 1. Check current state
DESCRIBE expenses;
SELECT COUNT(*) FROM expenses;

-- 2. Backup
DROP TABLE IF EXISTS expenses_backup;
CREATE TABLE expenses_backup AS SELECT * FROM expenses;
SELECT COUNT(*) FROM expenses_backup;

-- 3. Fix
TRUNCATE TABLE expenses;
ALTER TABLE expenses MODIFY COLUMN id bigint NOT NULL AUTO_INCREMENT;

-- 4. Verify
DESCRIBE expenses;
SELECT COUNT(*) FROM expenses;
```

Then upload the 3 Python files and restart the app!
