diff --git a/TblEditor.php b/TblEditor.php index bb695cc..ef08898 100644 --- a/TblEditor.php +++ b/TblEditor.php @@ -7,55 +7,93 @@ require_once './config.php'; require_once './_pdo.php'; require_once './src/D2Functions.php'; -require_once './src/D2Crud.php'; -error_reporting(E_ERROR | E_PARSE); +error_reporting(E_ALL); set_time_limit(-1); ini_set('max_input_time', '-1'); ini_set('max_execution_time', '0'); define('DB_FILE', $_SESSION['modname'] . ".db"); -PDO_Connect("sqlite:" . DB_FILE); - -if ($_SERVER['REQUEST_METHOD'] === 'POST') { - // Check if the "Add" button was clicked - if (isset($_POST['add'])) { - $tableName = $_POST['tableName']; - - // Retrieve the Key and String values from the form - $key = $_POST['key']; - $string = $_POST['string']; - - // Insert the new row into the table - $inserted = PDO_Execute("INSERT INTO $tableName (Key, String) VALUES (?, ?)", array($key, $string)); - - if ($inserted) { - // Redirect or display success message - header('Location: /TblEditorGUI.php'); - } else { - // Handle the insertion failure - echo PDO_ErrorInfo(); - } - } +try { + PDO_Connect("sqlite:" . DB_FILE); +} catch (Exception $e) { + echo "Connection error: " . $e->getMessage(); } -// Handle the DELETE submission - // Check if the "Delete" button was clicked - if ($_GET['cmd'] == "delete") { - - $tableName = $_GET['t']; - $key = $_GET['k']; - - // Delete the row from the table based on the Key - $deleted = PDO_Execute("DELETE FROM $tableName WHERE Key = ?", array($key)); - - if ($deleted) { - // Redirect or display success message - echo "Deleted $key from $tableName"; - } else { - // Handle the deletion failure - echo PDO_ErrorInfo(); +$cmd = $_REQUEST['cmd']; +$tableName = $_REQUEST['tableName']; - } +if ($cmd == "getTable") { + $query = "SELECT ROWID,* FROM $tableName"; + $rows = PDO_Execute($query); + + // Start the table with Bootstrap classes + $tableMarkup = ''; + + // Iterate over the rows + foreach ($rows as $row) { + $tableMarkup .= ''; + + $tableMarkup .= "{$row['rowid']}"; + + // Display the Key column as a text input with Bootstrap form-control class + $tableMarkup .= ''; + + // Display the String column as a textarea with Bootstrap form-control class + $tableMarkup .= ''; + + // Add the Update and Delete links within a single td element + $tableMarkup .= '
'; + + // Add the Update link with Bootstrap btn and btn-primary classes + $tableMarkup .= 'Update'; + + // Add the Delete link with Bootstrap btn and btn-danger classes + $tableMarkup .= 'Delete'; + + $tableMarkup .= '
'; + + $tableMarkup .= ''; } + + + // Return the table markup + echo $tableMarkup; +} + + +if ($cmd == "update") { + $query = "UPDATE patchstring SET Key = ?, String = ? WHERE ROWID = ?"; + PDO_Execute($query, [$_REQUEST['key'], $_REQUEST['string'], $_REQUEST['rowid']]); + + echo $_REQUEST['key'] . " updated"; +} + +if ($cmd == "delete") { + $query = "DELETE FROM $tableName WHERE ROWID = ?"; + PDO_Execute($query, [$_REQUEST['rowid']]); + echo $_REQUEST['rowid'] . " deleted"; +} + +if ($cmd == "add") { + $tableName = $_POST['tableName']; + $key = $_POST['Key']; + $string = $_POST['String']; + + // Create your insert statement using the provided values + $query = "INSERT INTO $tableName (Key, String) VALUES (?, ?)"; + $params = array($key, $string); + + // Execute the insert statement + $result = PDO_Execute($query, $params); + + + // Retrieve the last inserted rowid from the table + $query = "SELECT last_insert_rowid() AS rowid"; + $result = PDO_FetchOne($query); + + + echo $result; + +} diff --git a/TblEditorGUI.php b/TblEditorGUI.php index 4c6df33..d75ad61 100644 --- a/TblEditorGUI.php +++ b/TblEditorGUI.php @@ -1,5 +1,4 @@ readAll(); -} ?> - - - - - - Editable Tables - - -
-

Editable Tables

- -
- - -
-
-
-
- - -
-
- -
-
- -
-
-
-
- - - - - - - - - - - $row){ ?> - - - - - - - - -
IDKeyStringActions
- - - - -
-
-
- - + + + + + + + + + + + + + + + + + + + + + Editable Tables + + +
+

Select .tbl File

+ +

Table

+
+ Add new String + + +
+
+ + +
+ +
+
+ +
+ + + + + + + + + + + + +
IDKeyString
+
+ +
-
- - - - + +// Delete link click event handler + $(document).on('click', '.delete-link', function (event) { + event.preventDefault(); + + var selectedTable = $('select[name="tableName"]').val(); + + // Get the rowid from the custom attribute data-rowid + var rowid = $(this).data('rowid'); + + // Remove the deleted row from the table + $('.row-' + rowid).remove(); + + // Send GET request to TblEditor.php?cmd=delete&rowid={rowid} + $.get('TblEditor.php', {cmd: 'delete', tableName: selectedTable, rowid: rowid}, function (response) { + // Handle the response here + + // Wait until the response is loaded, then remove the row + $(response).ready(function () { + + // Update DataTable + + }); + }); + }); + + + + // Update link click event handler + $(document).on('click', '.update-link', function (event) { + event.preventDefault(); + + var selectedTable = $('select[name="tableName"]').val(); + + // Get the rowid from the custom attribute data-rowid + var rowid = $(this).data('rowid'); + + // Get the updated values from the corresponding input and textarea + var key = $('.row-' + rowid + ' input').val(); + var string = $('.row-' + rowid + ' textarea').val(); + + // Send POST request to TblEditor.php with the updated values + $.post('TblEditor.php', {cmd: 'update', tableName: selectedTable, rowid: rowid, key: key, string: string}, function (response) { + // Handle the response here + + // Optional: Update any UI elements if needed + + // Update DataTable + $('.table').DataTable().draw(); + }); + }); + + // Add link click event handler + $('#addLink').click(function () { + var selectedTable = $('#tableName').val(); + var key = $('#inputKey').val(); + var string = $('#inputString').val(); + + // Send POST request to TblEditor.php with parameters + $.post('TblEditor.php', {cmd: 'add', tableName: selectedTable, Key: key, String: string}, function (response) { + // Handle the response here + rowid = response; + // Create the HTML markup for the new row +var newRow = '' + + '' + rowid + '' + + '' + + '' + + '
' + + 'Update' + + 'Delete' + + '
' + + ''; +; + + + // Append the new row to the table + $('.table').DataTable().destroy(); + + $('.table tbody').append(newRow); + + $('.table').DataTable(); + }); + }); + + + }); + + + + + + + + \ No newline at end of file diff --git a/src/D2Crud.php b/src/D2Crud.php index 7a4dfdc..c495fd1 100644 --- a/src/D2Crud.php +++ b/src/D2Crud.php @@ -1,6 +1,6 @@ table} SET $setClause WHERE ROWID = ?"; - + $query = "UPDATE {$this->table} SET $setClause WHERE ROWID = ?"; + return PDO_Execute($query, $params); }