edit_row.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. session_start();
  3. // Enable error logging
  4. ini_set('display_errors', 1);
  5. ini_set('display_startup_errors', 1);
  6. error_reporting(E_ALL);
  7. // Load configuration
  8. $config = include(__DIR__ . '/../../config.php');
  9. // Get the website ID from the session
  10. $websiteId = $_SESSION['websiteId'] ?? 1;
  11. // Find the website configuration
  12. $websiteConfig = array_filter($config, function($site) use ($websiteId) {
  13. return isset($site['id']) && $site['id'] === $websiteId;
  14. });
  15. $websiteConfig = reset($websiteConfig);
  16. // Connect to SQLite database
  17. $db = new SQLite3($websiteConfig['database']);
  18. // Get the table and ID from the request
  19. $table = $_GET['table'] ?? '';
  20. $id = $_GET['id'] ?? 0;
  21. // Debug: Get the table schema
  22. $schema = $db->querySingle("SELECT sql FROM sqlite_master WHERE name='$table';");
  23. // Check if the ID exists
  24. $checkId = $db->querySingle('SELECT COUNT(*) FROM "' . $table . '" WHERE "id" = ' . (int) $id);
  25. if ($checkId == 0) {
  26. $idError = "No row found for ID $id in table $table.";
  27. }
  28. // Fetch columns
  29. $columns = [];
  30. $columnsResult = $db->query('PRAGMA table_info("' . $table . '")');
  31. while ($row = $columnsResult->fetchArray(SQLITE3_ASSOC)) {
  32. $columns[] = $row['name'];
  33. }
  34. // Fetch row
  35. $stmt = $db->prepare('SELECT * FROM "' . $table . '" WHERE "id" = :id');
  36. $stmt->bindValue(':id', (int) $id, SQLITE3_INTEGER);
  37. $result = $stmt->execute();
  38. $row = $result ? $result->fetchArray(SQLITE3_ASSOC) : null;
  39. // Handle form submission
  40. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_changes'])) {
  41. foreach ($columns as $column) {
  42. if (!isset($_POST[$column])) continue;
  43. $value = $_POST[$column] ?: null;
  44. if ($column === 'date' && empty($value)) {
  45. $value = date('Y-m-d H:i:s');
  46. }
  47. $updateStmt = $db->prepare('UPDATE "' . $table . '" SET "' . $column . '" = :value WHERE "id" = :id');
  48. $updateStmt->bindValue(':id', (int) $id, SQLITE3_INTEGER);
  49. $updateStmt->bindValue(':value', $value, SQLITE3_TEXT);
  50. $updateStmt->execute();
  51. }
  52. header("Location: edit_database.php?table=" . urlencode($table));
  53. exit();
  54. }
  55. // Handle Fix IDs
  56. if (isset($_POST['fix_ids'])) {
  57. // Step 1: Create a backup table
  58. $db->exec('CREATE TABLE IF NOT EXISTS "content_backup" AS SELECT * FROM "content"');
  59. // Step 2: Add a new ID column with auto-increment
  60. $db->exec('ALTER TABLE "content" RENAME TO "content_old"');
  61. $db->exec('CREATE TABLE "content" (
  62. id INTEGER PRIMARY KEY AUTOINCREMENT,
  63. page TEXT,
  64. title TEXT,
  65. content TEXT,
  66. date TEXT,
  67. parent TEXT
  68. )');
  69. // Step 3: Copy data and reassign IDs
  70. $db->exec('INSERT INTO "content" (page, title, content, date, parent)
  71. SELECT page, title, content, date, parent FROM "content_old"');
  72. // Step 4: Remove old table
  73. $db->exec('DROP TABLE "content_old"');
  74. header("Location: edit_database.php?table=content&fix_success=1");
  75. exit();
  76. }
  77. ?>
  78. <!DOCTYPE html>
  79. <html lang="en">
  80. <head>
  81. <meta charset="UTF-8">
  82. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  83. <title>Edit Row</title>
  84. </head>
  85. <body>
  86. <h1>Edit Row in Table: <?php echo htmlspecialchars($table); ?></h1>
  87. <?php if (isset($idError)): ?>
  88. <p style="color: red;"><?php echo $idError; ?></p>
  89. <form method="post">
  90. <button type="submit" name="fix_ids">Fix ID Issues</button>
  91. </form>
  92. <?php else: ?>
  93. <form method="post">
  94. <?php foreach ($columns as $column): ?>
  95. <label for="<?php echo htmlspecialchars($column); ?>"><?php echo htmlspecialchars($column); ?>:</label>
  96. <?php if ($column === 'content'): ?>
  97. <textarea id="<?php echo htmlspecialchars($column); ?>" name="<?php echo htmlspecialchars($column); ?>" style="resize: both;"><?php echo htmlspecialchars($row[$column] ?? '', ENT_QUOTES, 'UTF-8'); ?></textarea>
  98. <?php else: ?>
  99. <input type="text" id="<?php echo htmlspecialchars($column); ?>" name="<?php echo htmlspecialchars($column); ?>" value="<?php echo htmlspecialchars($row[$column] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
  100. <?php endif; ?>
  101. <br>
  102. <?php endforeach; ?>
  103. <button type="submit" name="save_changes">Save Changes</button>
  104. </form>
  105. <?php endif; ?>
  106. <a href="edit_database.php?table=<?php echo htmlspecialchars($table); ?>">Back to Table</a>
  107. </body>
  108. </html>