beginTransaction(); $transactionActive = true; // Disable foreign key checks to avoid constraint issues $db->exec('SET FOREIGN_KEY_CHECKS = 0'); foreach ($tables as $table) { // Sanitize table name to prevent SQL injection $table = preg_replace('/[^a-zA-Z0-9_]/', '', $table); $db->exec("TRUNCATE TABLE `$table`"); echo "Table `$table` truncated successfully.\n"; } // Re-enable foreign key checks $db->exec('SET FOREIGN_KEY_CHECKS = 1'); $db->commit(); $transactionActive = false; echo "All specified tables have been truncated successfully.\n"; } catch (Exception $e) { if ($db !== null && $transactionActive) { $db->rollBack(); } logError('Error truncating tables: ' . $e->getMessage()); echo "Error truncating tables: " . $e->getMessage() . "\n"; throw $e; // Re-throw to allow debugging } } // Execute the truncation try { truncateTables($tablesToTruncate); } catch (Exception $e) { // Handle or log the error as needed echo "Fatal error: " . $e->getMessage() . "\n"; } ?>