소스 검색

worst way to fix it but IDs wont break as badly anymore

Kablersalat 9 달 전
부모
커밋
1d29d12f23
2개의 변경된 파일82개의 추가작업 그리고 33개의 파일을 삭제
  1. 1 2
      admin/tools/edit_database.php
  2. 81 31
      admin/tools/edit_row.php

+ 1 - 2
admin/tools/edit_database.php

@@ -249,6 +249,7 @@ if (isset($_POST['reassign_ids'])) {
                                         <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" disabled>
                                     <?php elseif ($column == 'date'): ?>
                                         <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" value="<?php echo date('Y-m-d H:i:s'); ?>" disabled>
+                                    <?php else: ?>
                                         <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]">
                                     <?php endif; ?>
                                 </td>
@@ -337,8 +338,6 @@ if (isset($_POST['reassign_ids'])) {
                                         <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" disabled>
                                     <?php elseif ($column == 'date'): ?>
                                         <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" value="<?php echo date('Y-m-d H:i:s'); ?>" disabled>
-                                    <?php elseif ($selectedTable == 'content' && $column == 'custom_html'): ?>
-                                        <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" value="0">
                                     <?php else: ?>
                                         <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]">
                                     <?php endif; ?>

+ 81 - 31
admin/tools/edit_row.php

@@ -1,6 +1,11 @@
 <?php
 session_start();
 
+// Enable error logging
+ini_set('display_errors', 1);
+ini_set('display_startup_errors', 1);
+error_reporting(E_ALL);
+
 // Load configuration
 $config = include(__DIR__ . '/../../config.php');
 
@@ -20,40 +25,76 @@ $db = new SQLite3($websiteConfig['database']);
 $table = $_GET['table'] ?? '';
 $id = $_GET['id'] ?? 0;
 
+// Debug: Get the table schema
+$schema = $db->querySingle("SELECT sql FROM sqlite_master WHERE name='$table';");
+
+// Check if the ID exists
+$checkId = $db->querySingle('SELECT COUNT(*) FROM "' . $table . '" WHERE "id" = ' . (int) $id);
+if ($checkId == 0) {
+    $idError = "No row found for ID $id in table $table.";
+}
+
+// Fetch columns
 $columns = [];
-$columnsResult = $db->query("PRAGMA table_info($table)");
+$columnsResult = $db->query('PRAGMA table_info("' . $table . '")');
 while ($row = $columnsResult->fetchArray(SQLITE3_ASSOC)) {
     $columns[] = $row['name'];
 }
 
-$stmt = $db->prepare("SELECT * FROM $table WHERE id = :id");
-$stmt->bindValue(':id', $id, SQLITE3_INTEGER);
+// Fetch row
+$stmt = $db->prepare('SELECT * FROM "' . $table . '" WHERE "id" = :id');
+$stmt->bindValue(':id', (int) $id, SQLITE3_INTEGER);
 $result = $stmt->execute();
 
-if ($result) {
-    $row = $result->fetchArray(SQLITE3_ASSOC);
-} else {
-    $row = null;
-}
+$row = $result ? $result->fetchArray(SQLITE3_ASSOC) : null;
 
-if (isset($_POST['save_changes'])) {
+// Handle form submission
+if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_changes'])) {
     foreach ($columns as $column) {
+        if (!isset($_POST[$column])) continue;
+
         $value = $_POST[$column] ?: null;
-        if ($column == 'date' && empty($value)) {
+        if ($column === 'date' && empty($value)) {
             $value = date('Y-m-d H:i:s');
         }
-        if ($table == 'content' && $column == 'custom_html') {
-            $value = $value ? 1 : 0;
-        }
-        $stmt = $db->prepare("UPDATE $table SET $column = :value WHERE id = :id");
-        $stmt->bindValue(':value', $value, SQLITE3_TEXT);
-        $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
-        $stmt->execute();
+
+        $updateStmt = $db->prepare('UPDATE "' . $table . '" SET "' . $column . '" = :value WHERE "id" = :id');
+        $updateStmt->bindValue(':id', (int) $id, SQLITE3_INTEGER);
+        $updateStmt->bindValue(':value', $value, SQLITE3_TEXT);
+        $updateStmt->execute();
     }
-    header("Location: edit_database.php?table=$table");
+    header("Location: edit_database.php?table=" . urlencode($table));
+    exit();
+}
+
+// Handle Fix IDs
+if (isset($_POST['fix_ids'])) {
+    // Step 1: Create a backup table
+    $db->exec('CREATE TABLE IF NOT EXISTS "content_backup" AS SELECT * FROM "content"');
+
+    // Step 2: Add a new ID column with auto-increment
+    $db->exec('ALTER TABLE "content" RENAME TO "content_old"');
+    $db->exec('CREATE TABLE "content" (
+        id INTEGER PRIMARY KEY AUTOINCREMENT,
+        page TEXT,
+        title TEXT,
+        content TEXT,
+        date TEXT,
+        parent TEXT
+    )');
+
+    // Step 3: Copy data and reassign IDs
+    $db->exec('INSERT INTO "content" (page, title, content, date, parent)
+               SELECT page, title, content, date, parent FROM "content_old"');
+
+    // Step 4: Remove old table
+    $db->exec('DROP TABLE "content_old"');
+
+    header("Location: edit_database.php?table=content&fix_success=1");
     exit();
 }
 ?>
+
 <!DOCTYPE html>
 <html lang="en">
 <head>
@@ -63,18 +104,27 @@ if (isset($_POST['save_changes'])) {
 </head>
 <body>
     <h1>Edit Row in Table: <?php echo htmlspecialchars($table); ?></h1>
-    <form method="post">
-        <?php foreach ($columns as $column): ?>
-            <label for="<?php echo htmlspecialchars($column); ?>"><?php echo htmlspecialchars($column); ?>:</label>
-            <?php if ($table == 'content' && $column == 'content'): ?>
-                <textarea id="<?php echo htmlspecialchars($column); ?>" name="<?php echo htmlspecialchars($column); ?>" style="resize: both;"><?php echo htmlspecialchars($row[$column] ?? ''); ?></textarea>
-            <?php else: ?>
-                <input type="text" id="<?php echo htmlspecialchars($column); ?>" name="<?php echo htmlspecialchars($column); ?>" value="<?php echo htmlspecialchars($row[$column] ?? ''); ?>">
-            <?php endif; ?>
-            <br>
-        <?php endforeach; ?>
-        <button type="submit" name="save_changes">Save Changes</button>
-    </form>
+
+    <?php if (isset($idError)): ?>
+        <p style="color: red;"><?php echo $idError; ?></p>
+        <form method="post">
+            <button type="submit" name="fix_ids">Fix ID Issues</button>
+        </form>
+    <?php else: ?>
+        <form method="post">
+            <?php foreach ($columns as $column): ?>
+                <label for="<?php echo htmlspecialchars($column); ?>"><?php echo htmlspecialchars($column); ?>:</label>
+                <?php if ($column === 'content'): ?>
+                    <textarea id="<?php echo htmlspecialchars($column); ?>" name="<?php echo htmlspecialchars($column); ?>" style="resize: both;"><?php echo htmlspecialchars($row[$column] ?? '', ENT_QUOTES, 'UTF-8'); ?></textarea>
+                <?php else: ?>
+                    <input type="text" id="<?php echo htmlspecialchars($column); ?>" name="<?php echo htmlspecialchars($column); ?>" value="<?php echo htmlspecialchars($row[$column] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
+                <?php endif; ?>
+                <br>
+            <?php endforeach; ?>
+            <button type="submit" name="save_changes">Save Changes</button>
+        </form>
+    <?php endif; ?>
+
     <a href="edit_database.php?table=<?php echo htmlspecialchars($table); ?>">Back to Table</a>
 </body>
-</html>
+</html>