exec($sql_add_column); // Add foreign key constraint $sql_add_fk = "ALTER TABLE warranty_registrations ADD CONSTRAINT fk_sold_serial FOREIGN KEY (sold_serial_id) REFERENCES sold_serials(id) ON DELETE SET NULL"; $pdo->exec($sql_add_fk); echo "Table 'warranty_registrations' modified successfully." . PHP_EOL; } catch (PDOException $e) { // Check if column already exists to avoid fatal error on re-run if (strpos($e->getMessage(), 'Duplicate column name') === false) { die("DB ERROR: " . $e->getMessage()); } echo "Column 'sold_serial_id' already exists in 'warranty_registrations'." . PHP_EOL; }