edit_row.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. session_start();
  3. // Load configuration
  4. $config = include(__DIR__ . '/../../config.php');
  5. // Get the website ID from the session
  6. $websiteId = $_SESSION['websiteId'] ?? 1;
  7. // Find the website configuration
  8. $websiteConfig = array_filter($config, function($site) use ($websiteId) {
  9. return isset($site['id']) && $site['id'] === $websiteId;
  10. });
  11. $websiteConfig = reset($websiteConfig);
  12. // Connect to SQLite database
  13. $db = new SQLite3($websiteConfig['database']);
  14. // Get the table and ID from the request
  15. $table = $_GET['table'] ?? '';
  16. $id = $_GET['id'] ?? 0;
  17. $columns = [];
  18. $columnsResult = $db->query("PRAGMA table_info($table)");
  19. while ($row = $columnsResult->fetchArray(SQLITE3_ASSOC)) {
  20. $columns[] = $row['name'];
  21. }
  22. $stmt = $db->prepare("SELECT * FROM $table WHERE id = :id");
  23. $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
  24. $result = $stmt->execute();
  25. if ($result) {
  26. $row = $result->fetchArray(SQLITE3_ASSOC);
  27. } else {
  28. $row = null;
  29. }
  30. if (isset($_POST['save_changes'])) {
  31. foreach ($columns as $column) {
  32. $value = $_POST[$column] ?: null;
  33. if ($column == 'date' && empty($value)) {
  34. $value = date('Y-m-d H:i:s');
  35. }
  36. if ($table == 'content' && $column == 'custom_html') {
  37. $value = $value ? 1 : 0;
  38. }
  39. $stmt = $db->prepare("UPDATE $table SET $column = :value WHERE id = :id");
  40. $stmt->bindValue(':value', $value, SQLITE3_TEXT);
  41. $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
  42. $stmt->execute();
  43. }
  44. header("Location: edit_database.php?table=$table");
  45. exit();
  46. }
  47. ?>
  48. <!DOCTYPE html>
  49. <html lang="en">
  50. <head>
  51. <meta charset="UTF-8">
  52. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  53. <title>Edit Row</title>
  54. </head>
  55. <body>
  56. <h1>Edit Row in Table: <?php echo htmlspecialchars($table); ?></h1>
  57. <form method="post">
  58. <?php foreach ($columns as $column): ?>
  59. <label for="<?php echo htmlspecialchars($column); ?>"><?php echo htmlspecialchars($column); ?>:</label>
  60. <?php if ($table == 'content' && $column == 'content'): ?>
  61. <textarea id="<?php echo htmlspecialchars($column); ?>" name="<?php echo htmlspecialchars($column); ?>" style="resize: both;"><?php echo htmlspecialchars($row[$column] ?? ''); ?></textarea>
  62. <?php else: ?>
  63. <input type="text" id="<?php echo htmlspecialchars($column); ?>" name="<?php echo htmlspecialchars($column); ?>" value="<?php echo htmlspecialchars($row[$column] ?? ''); ?>">
  64. <?php endif; ?>
  65. <br>
  66. <?php endforeach; ?>
  67. <button type="submit" name="save_changes">Save Changes</button>
  68. </form>
  69. <a href="edit_database.php?table=<?php echo htmlspecialchars($table); ?>">Back to Table</a>
  70. </body>
  71. </html>