I found a javascript/ajax script that I used several years ago for inline editing of a customer list and it still works perfectly. I thought I would re-purpose this to make a bank ledger but for the life of me I can not get this thing to work. I took the original script and changed the variables to the new ones, created a new DB and table and updated the PHP part of the script, but it just doesn’t seem like the PHP file that actually writes the data is being accessed. I get no errors, it just reverts back to the old value when the screen refreshes. I have struggled for 4 days with this and thought, OK, I am just going to take the old script and modify it again from scratch. Same issue. I have checked the code over and over and over and see no mistakes. I have deleted the DB and recreated it. I have checked permissions on files and everything is the same. I just don’t have a clue where to even look next. Hope someone can help. The PHP file is scaled down and has no error correction, my main concern is just getting it to access it and write the data.
Thanks
<?php
if(session_status() == PHP_SESSION_NONE) {
session_start();
}
//if(empty($_SESSION["admin"])) { header("location: index.php"); exit; }
include("include/connect.php");
include("include/define.php");
include("header.php");
$_SESSION['loggedin'] = true;
$_SESSION['id'] = "37";
$stmt = $pdo->prepare("SELECT account FROM customers WHERE id = ? Limit 1");
$stmt->execute([$_SESSION['id']]);
$data = $stmt->fetch();
$stmt = null;
$account = $_SESSION['account'] = $data['account'];
?>
<div class="menu"><?php include("mainmenu.php"); ?></div>
<div class="content">
<?php if(isset($_SESSION['message'])) { echo "<div class='sessionmessage'>".$_SESSION['message']."</div>"; unset($_SESSION['message']); } else { echo "<div class='pageheading'><b>Manage Customers</b></div>"; } ?>
<div class="tableheading"><b>Add Record</b></div>
<table class="tables nosort-add">
<tr>
<th style="width: 125px"><strong>Date</strong></th>
<th style="width: 45px"><strong>CHQ</strong></th>
<th style="width: 390px"><strong>Payee</strong></th>
<th style="width: 390px" class="tcenter"><strong>Category</strong></th>
<th style="width: 95px" class="tcenter"><strong>Credit</strong></th>
<th style="width: 95px" class="tcenter"><strong>Debit</strong></th>
<th style="width: 20px" class="tcenter"><strong>CLR</strong></th>
<th style="width: 20px" class="tcenter"><strong>Save</strong></th>
</tr>
<tr>
<td style="width: 125px" id="thisdate" contenteditable></td>
<td style="width: 45px" id="cheque" contenteditable></td>
<td style="width: 390px" id="payee" contenteditable></td>
<td style="width: 390px" id="category" contenteditable></td>
<td style="width: 95px" id="credit" contenteditable></td>
<td style="width: 95px" id="debit" contenteditable></td>
<td style="width: 20px" id="cleared" contenteditable></td>
<td class="tcenter" style="width: 20px"><div class="tooltip"><button type="button" name="btn_add" id="btn_add"><i class="fa fa-plus" style="color: green; font-size: 16px"></i></button><span class="tooltiptext">Add Customer</span></div></td>
</tr>
</table>
<br><br>
<div style="width: 1180px; margin: auto auto">
<?php
if(isset($_POST['limits'])) { $limit = $_POST['limits']; } else { $limit = 10; }
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page = 1; };
$start_from = ($page-1) * $limit;
$data = $pdo->query('select count(*) from '.$account)->fetchColumn();
$total_records = $data;
$total_pages = ceil($total_records / $limit);
if(!$total_pages) { $total_pages = 1; }
elseif($page > $total_pages) { header("location: index.php"); }
$pageLink = "<div class='srt'> Pages  ";
for ($i=1; $i <= $total_pages; $i++) {
$pageLink .= "<a href='editcustomers.php?page=".$i."'>".$i." </a>";
};
?>
<?php echo $pageLink . "<span style='color: #83A1CD; font-size: 11px'> | Click heading to sort by that column</span></div>"; ?><br>
<div style="float: left">
<form action="editcustomers.php" method="post">
<select name="limits" id="limits">
<option selected value="0" disabled>Posts per Page</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
<option value="50">50</option>
</select>
</form>
</div>
<div style="float: right">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input class="search form-control" type="text" name="search" placeholder="Search" value="">
<input name="submit" value="Submit" type="image" src="images/search.png" width="20" height="20" class="magnify">
</form>
</div>
<div style="clear: both"></div>
</div>
<div class="tableheading"><b>Edit / Delete Record</b></div>
<table class="tables sortable">
<thead>
<tr>
<th style="width: 105px"><strong>Date</strong></th>
<th style="width: 45px"><strong>CHQ</strong></th>
<th style="width: 360px"><strong>Payee</strong></th>
<th style="width: 360px" class="tcenter"><strong>Category</strong></th>
<th style="width: 95px" class="tcenter"><strong>Credit</strong></th>
<th style="width: 95px" class="tcenter"><strong>Debit</strong></th>
<th style="width: 95px" class="tcenter"><strong>BAL</strong></th>
<th style="width: 20px" class="tcenter"><strong>CLR</strong></th>
<th class="no-sort" style="width: 20px" class="tcenter"><strong>Action</strong></th>
</tr>
</thead>
<tbody>
<?php
$thisdate=$cheque=$payee=$category=$credit=$debit=$cleared=$total = "";
if(isset($_POST['search'])) {
$search = filter_var($_POST['search'], FILTER_SANITIZE_STRING);
$stmt = $pdo->prepare("SELECT * FROM ".$account." WHERE thisdate LIKE ('%".$search."%') or cheque LIKE ('%".$search."%') or payee LIKE ('%".$search."%') or category LIKE ('%".$search."%') or credit LIKE ('%".$search."%') or debit LIKE ('%".$search."%') ORDER BY id ASC LIMIT $start_from, $limit");
}
else {
$stmt = $pdo->prepare("SELECT * FROM ".$account." WHERE year(thisdate) = year(NOW()) and month(thisdate) = month(NOW()) ORDER BY id ASC LIMIT $start_from, $limit");
}
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt = null;
foreach($row as $row) {
?>
<tr>
<td class="thisdate" data-id1="<?php echo $row["id"]; ?>" contenteditable style="width: 105px"><?php echo $row["thisdate"]; ?></td>
<td class="cheque" data-id2="<?php echo $row["id"]; ?>" contenteditable style="width: 45px"><?php echo $row["cheque"]; ?></td>
<td class="payee" data-id3="<?php echo $row["id"]; ?>" contenteditable style="width: 360px"><?php echo $row["payee"]; ?></td>
<td class="category" data-id4="<?php echo $row["id"]; ?>" contenteditable style="width: 360px"><?php echo $row["category"]; ?></td>
<td class="credit" data-id5="<?php echo $row["id"]; ?>" contenteditable style="width: 95px"><?php echo $row["credit"]; ?></td>
<td class="debit" data-id6="<?php echo $row["id"]; ?>" contenteditable style="width: 95px"><?php echo $row["debit"]; ?></td>
<td class="total" data-id7="<?php echo $row["id"]; ?>" contenteditable style="width: 95px"><?php echo $row["total"]; ?></td>
<td class="cleared" data-id8="<?php echo $row["id"]; ?>" contenteditable style="width: 20px"><?php echo $row["cleared"]; ?></td>
<td class="tcenter" style="width: 20px"><div class="tooltip"><button type='button' name='delete_btn' data-id9=<?php echo $row["id"]; ?> class='btn btn-xs btn-danger btn_delete'><i class="fas fa-trash-alt" style="color: maroon; font-size: 16px; font-weight: normal"></i></button><span class="tooltiptext">Delete Record</span></div></td>
</tr>
<?php }?>
</tbody>
</table>
<br>
<?php echo "<div style='width: 1180px; margin: auto auto'><b>Page ".$page." of ".$total_pages."</b></div><br><br>"; ?>
</div>
<?php include("footer.php"); ?>
</body>
</html>
<script type="text/javascript">
jQuery(function() {
jQuery('#limits').change(function() {
this.form.submit();
});
});
</script>
<script>
$(document).ready(function(){
function fetch_data()
{
$.ajax({
url:"editcustomers.php",
method:"POST"
});
}
fetch_data();
$(document).on('click', '#btn_add', function(){
var thisdate = $('#thisdate').text();
var cheque = $('#cheque').text();
var payee = $('#payee').text();
var category = $('#category').text();
var credit = $('#credit').text();
var debit = $('#debit').text();
var total= $('#total').text();
var cleared = $('#cleared').text();
$.ajax({
url:"inupdelete.php?action=insert",
method:"POST",
data:{thisdate:thisdate, cheque:cheque, payee:payee, category:category, credit:credit, debit:debit, total:total, cleared:cleared},
dataType:"text",
success:function(data)
{
location.reload();
}
})
});
function edit_data(id, text, column_name)
{
$.ajax({
url:"inupdelete.php?action=update",
method:"POST",
data:{id:id, text:text, column_name:column_name},
dataType:"text",
success:function(data)
{
location.reload();
}
});
}
$(document).on('blur', '.thisdate', function(){
var id = $(this).data("id1");
var thisdate = $(this).text();
edit_data(id, thisdate, "thisdate");
});
$(document).on('blur', '.cheque', function(){
var id = $(this).data("id2");
var cheque = $(this).text();
edit_data(id, cheque, "cheque");
});
$(document).on('blur', '.payee', function(){
var id = $(this).data("id3");
var payee = $(this).text();
edit_data(id, payee, "payee");
});
$(document).on('blur', '.category', function(){
var id = $(this).data("id4");
var category = $(this).text();
edit_data(id, category, "category");
});
$(document).on('blur', '.credit', function(){
var id = $(this).data("id5");
var credit = $(this).text();
edit_data(id, credit, "credit");
});
$(document).on('blur', '.debit', function(){
var id = $(this).data("id6");
var debit = $(this).text();
edit_data(id, debit, "debit");
});
$(document).on('blur', '.total', function(){
var id = $(this).data("id7");
var total = $(this).text();
edit_data(id, total, "total");
});
$(document).on('blur', '.cleared', function(){
var id = $(this).data("id8");
var cleared = $(this).text();
edit_data(id, cleared, "cleared");
});
$(document).on('click', '.btn_delete', function(){
var id=$(this).data("id9");
if(confirm("Are you sure you want to delete this record?"));
$.ajax({
url:"inupdelete.php?action=delete",
method:"POST",
data:{id:id},
dataType:"text",
success:function(data){
location.reload();
}
});
});
});
</script>
<?php
if(session_status() == PHP_SESSION_NONE){
session_start();
}
$action = $_GET["action"]; $account = $_SESSION["account"];
if($action === "update") { UpdateColumn($account); }
if($action === "delete") { DeleteRecord($account); }
if($action === "insert") { InsertRecord($account); }
function UpdateColumn($account) {
$stmt = $pdo->prepare("UPDATE ".$account." SET ".$thiscolumn." = '".$thisvalue."' WHERE id = ?");
if($stmt->execute([$_POST['id']])) {
$_SESSION['message'] = "Column updated successfully.";
}
else {
$_SESSION['message'] = "Error updating Record";
}
}
function DeleteRecord($account) {
include "include/connect.php";
$stmt = $pdo->prepare("DELETE FROM ".$account." WHERE id = ? LIMIT 1");
$stmt->execute([$_POST['id']]);
if ($stmt->rowCount()) {
$_SESSION['message'] = "Record deleted successfully.";
}
else { $_SESSION['message'] = "Record not deleted, try again.";
}
function InsertRecord($account) { echo "TESTING";
include "include/connect.php";
$thisdate = $_POST["thisdate"];
if(empty($_POST["cheque")) { $cheque = NULL; } else { $cheque = $_POST["cheque"]; }
$thisdate = $_POST['payee'];
$thisdate = $_POST['category'];
if(empty($_POST["credit")) { $credit = NULL; } else { $credit = $_POST["credit"]; }
if(empty($_POST["debit")) { $debit = NULL; } else { $debit = $_POST["credit"]; }
$cleared = $_POST['cleared'];
$total = NULL;
$msql = "INSERT INTO ".$account." (thisdate, cheque, payee, category, credit, debit, total, cleared) VALUES (?,?,?,?,?,?,?,?)";
$stmt= $pdo->prepare($msql);
$stmt->execute([$thisdate, $cheque, $payee, $category, $credit, $debit, $cleared, $total);
if ($stmt->rowCount()) {
$_SESSION['message'] = "Record added successfully.";
}
else {
$_SESSION['message'] = "There was an error creating this record, please try again!";
}
$stmt = null;
}
?>