Code: Select all
<?php
$connect = mysqli_connect("localhost", "hmailserver", "supersecretpassword", "hmailserver");
if (isset($_GET['page'])) {
$page = $_GET['page'];
$display_pagination = 1;
} else {
$page = 1;
$total_pages = 1;
$display_pagination = 0;
}
$no_of_records_per_page = 10;
$offset = ($page-1) * $no_of_records_per_page;
$total_pages_sql = "SELECT Count( * ) AS count FROM hm_mylists";
$result_rows = mysqli_query($connect,$total_pages_sql);
$total_rows = mysqli_fetch_array($result_rows)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
$query = "SELECT * FROM hm_mylists ORDER BY id DESC LIMIT ".$offset.", ".$no_of_records_per_page;
$result = mysqli_query($connect,$query);
?>
<html>
<head>
<title>Live Table Data Edit Delete using Tabledit Plugin in PHP</title>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
<script src="jquery.min.js"></script>
<script src="bootstrap.min.js"></script>
<script src="jquery.tabledit.min.js"></script>
</head>
<body>
<div class="container">
<br />
<?php
if (isset($_GET['insert_function'])) {$insert_function = mysqli_real_escape_string($connect, $_GET['insert_function']);} else {$insert_function="";}
if (isset($_GET['insert_field'])) {$insert_field = mysqli_real_escape_string($connect, $_GET["insert_field"]);} else {$insert_field="";}
if (isset($_GET['insert_data'])) {$insert_data = mysqli_real_escape_string($connect, $_GET["insert_data"]);} else {$insert_data="";}
if (isset($_GET['insert_active'])) {$insert_active = mysqli_real_escape_string($connect, $_GET["insert_active"]);} else {$insert_active="";}
if ((!empty($insert_function)) && (!empty($insert_field)) && (!empty($insert_data))){
$insert_query = "INSERT INTO hm_mylists (function,field,data,hits,lastused,active) VALUES ('".$insert_function."','".$insert_field."','".$insert_data."',0,NOW(),'".$insert_active."');";
mysqli_query($connect, $insert_query);
header("Location: ./index.php");
}
?>
<table class="table">
<thead>
<tr>
<th>Function</th>
<th>Field</th>
<th>Data</th>
<th>Active</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<tr>
<form action='index.php' method='GET' onsubmit='return confirm(\"Are you sure you want to add this entry?\");'>
<td>
<input type='text' size='20' name='insert_function'>
</td>
<td>
<input type='text' size='20' name='insert_field'>
</td>
<td>
<input type='text' size='40' name='insert_data'>
</td>
<td>
<select name='insert_active'>
<option value=1>Yes</option>
<option value=0>No</option>
</select>
</td>
<td>
<input type='submit' name='submit' value='Submit' >
</td>
</form>
</tr>
</tbody>
</table>
<br />
<br />
<div class="table-responsive">
<h3 align="center">Live Table Data Edit Delete using Tabledit Plugin in PHP</h3><br />
<table id="editable_table" class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Function</th>
<th>Field</th>
<th>Data</th>
<th>Hits</th>
<th>Last Used</th>
<th>Active</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>'.$row["id"].'</td>
<td>'.$row["function"].'</td>
<td>'.$row["field"].'</td>
<td>'.$row["data"].'</td>
<td>'.$row["hits"].'</td>
<td>'.$row["lastused"].'</td>
<td>'.$row["active"].'</td>
</tr>
';
}
?>
</tbody>
</table>
<?php
if ($total_pages < 2){echo "";}
else {
echo "<ul class='pagination'>";
if($page <= 1){echo "<li class='page-item'><a class='page-link' href='#'>First </a></li>";} else {echo "<li class='page-item'><a class='page-link' href=\"index.php?page=1\">First </a><li>";}
if($page <= 1){echo "<li><a class='page-link' href='#'>Prev </a></li class='page-item'>";} else {echo "<li class='page-item'><a class='page-link' href=\"index.php?page=".($page - 1)."\">Prev </a></li>";}
if($page >= $total_pages){echo "<li class='page-item'><a class='page-link' href='#'>Next </a></li>";} else {echo "<li class='page-item'><a class='page-link' href=\"index.php?page=".($page + 1)."\">Next </a></li>";}
if($page >= $total_pages){echo "<li class='page-item'><a class='page-link' href='#'>Last</a></li>";} else {echo "<li class='page-item'><a class='page-link' href=\"index.php?page=".$total_pages."\">Last</a></li>";}
echo "</ul>";
}
if ($total_pages > 0){
echo "<br />";
}
?>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#editable_table').Tabledit({
url:'action.php',
columns:{
identifier:[0, "id"],
editable:[[1, 'function'], [2, 'field'], [3, 'data'], [6, 'active']]
},
restoreButton:false,
onSuccess:function(data, textStatus, jqXHR)
{
if(data.action == 'delete')
{
$('#'+data.id).remove();
}
}
});
});
</script>