<?php
// 1. Connect to the database
$LINK = mysqli_connect('127.0.0.1', 'tyrfl709', '[q8OXd*BEbxW6bM4', 'tyrfl709');
if (!$LINK) {
die("Database connection error: " . mysqli_connect_error());
}
// Optional: display errors during development
ini_set('display_errors', '1');
error_reporting(E_ALL);
// 2. If the Save button was clicked, update the selected page
if (isset($_POST['BUTTON_save'])) {
// Page name to update
$pageToSave = mysqli_real_escape_string($LINK, $_POST['DATA_page']);
// New content from the textarea
$newContent = mysqli_real_escape_string($LINK, $_POST['DATA_content']);
// Update the row in the "pages" table
$updateQuery = "UPDATE pages SET code = '$newContent' WHERE name = '$pageToSave'";
mysqli_query($LINK, $updateQuery);
}
// 3. Get the list of pages (for the dropdown)
$query = "SELECT name FROM pages";
$result = mysqli_query($LINK, $query);
$pages = [];
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$pages[] = $row['name'];
}
}
// 4. Determine which page is selected
// If nothing is posted yet, default to the first page in the list (if any)
$selectedPage = isset($_POST['DATA_page']) ? $_POST['DATA_page'] : ($pages[0] ?? '');
$selectedPageEscaped = mysqli_real_escape_string($LINK, $selectedPage);
// 5. Fetch the content of the selected page
$query2 = "SELECT code FROM pages WHERE name = '$selectedPageEscaped' LIMIT 1";
$result2 = mysqli_query($LINK, $query2);
$content = '';
if ($result2 && mysqli_num_rows($result2) > 0) {
$row2 = mysqli_fetch_assoc($result2);
$content = $row2['code'];
} else {
$content = "No content available.";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CMS Editor</title>
</head>
<body>
<h1>CMS Editor</h1>
<!-- CMS Form -->
<form action="cms.php" method="POST" id="form">
<!-- Dropdown to select a page to edit -->
<select name="DATA_page" onchange="document.getElementById('form').submit();">
<?php
foreach ($pages as $p) {
echo '<option value="' . $p . '"';
if ($p === $selectedPage) {
echo ' selected';
}
echo '>' . $p . '</option>';
}
?>
</select>
<br><br>
<!-- Textarea to edit the page content -->
<textarea name="DATA_content" rows="10" cols="50"><?php echo htmlspecialchars($content); ?></textarea>
<br><br>
<!-- Save button -->
<button type="submit" name="BUTTON_save">Save</button>
</form>
</body>
</html>