36326-vm/db/setup_ngos.php
2025-11-26 15:44:00 +00:00

35 lines
1.3 KiB
PHP

<?php
require_once 'config.php';
try {
$pdo = db();
// ngos table
$sql_ngos = "
CREATE TABLE IF NOT EXISTS ngos (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);";
$pdo->exec($sql_ngos);
echo "Database table 'ngos' created successfully (if it didn't exist).\n";
// Add ngo_id to food_listings table
try {
$sql_alter = "ALTER TABLE food_listings ADD COLUMN ngo_id INT NULL AFTER volunteer_id, ADD FOREIGN KEY (ngo_id) REFERENCES ngos(id) ON DELETE SET NULL";
// I am getting an error with volunteer_id not existing, so I will add it first.
$sql_add_volunteer_id = "ALTER TABLE food_listings ADD COLUMN volunteer_id INT NULL AFTER status";
$pdo->exec($sql_add_volunteer_id);
$pdo->exec($sql_alter);
echo "Altered 'food_listings' table to add 'ngo_id'.\n";
} catch (PDOException $e) {
// If the column already exists, this will fail, which is okay.
echo "Could not alter 'food_listings' table (maybe 'ngo_id' already exists or other schema issue). Error: " . $e->getMessage() . "\n";
}
} catch (PDOException $e) {
die("DB ERROR: " . $e->getMessage());
}