When i insert an item it is shown on the list 2 times, but on mysql table it is registered just one time. It started when i started using sessions. Thank you in advance.
index.php
<html>
<head>
<title>Live Table Data Edit</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="table-responsive">
<h3 align="center">Lista articulos prestados</h3><br />
<div id="live_data"></div>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
function fetch_data()
{
$.ajax({
url:"select.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
$(document).on('click', '#btn_add', function(){
var Articulo = $('#Articulo').text();
var fecha = $('#fecha').text();
var emailpresta = $('#emailpresta').text();
var emailrecibe = $('#emailrecibe').text();
if(Articulo == '')
{
alert("Ingresa Articulo");
return false;
}
if(fecha == '')
{
alert("Ingresa Fecha");
return false;
}
if(emailpresta == '')
{
alert("Ingresa tu email");
return false;
}
if(emailrecibe == '')
{
alert("Ingresa email tercero");
return false;
}
$.ajax({
url:"insert.php",
method:"POST",
data:{Articulo:Articulo, fecha:fecha, emailpresta:emailpresta, emailrecibe:emailrecibe },
dataType:"text",
success:function(data)
{
alert(data);
fetch_data();
}
})
});
$(document).on('click', '.btn_delete', function(){
var id=$(this).data("id3");
if(confirm("Are you sure you want to delete this?"))
{
$.ajax({
url:"delete.php",
method:"POST",
data:{id:id},
dataType:"text",
success:function(data){
alert(data);
fetch_data();
}
});
}
});
});
</script>
select.php
<?php
session_start();
$check_usuario = $_SESSION['email'];
$connect = mysqli_connect("######", "######", "", "############");
$output = '';
$sql = "SELECT * FROM articulos INNER JOIN usuarios WHERE emailpresta='".$check_usuario."'";
$result = mysqli_query($connect, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="10%">Id</th>
<th width="40%">Articulo</th>
<th width="40%">Fecha(Formato: AAAA-MM-DD)</th>
<th width="40%">Email-presta</th>
<th width="40%">Email-recibe</th>
<th width="10%">Borrar</th>
</tr>';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["id"].'</td>
<td class="Articulo" data-id1="'.$row["id"].'" contenteditable>'.$row["Articulo"].'</td>
<td class="fecha" data-id2="'.$row["id"].'" contenteditable>'.$row["fecha"].'</td>
<td class="emailpresta" data-id2="'.$row["id"].'" contenteditable>'.$row["emailpresta"].'</td>
<td class="emailrecibe" data-id2="'.$row["id"].'" contenteditable>'.$row["emailrecibe"].'</td>
<td><button type="button" name="delete_btn" data- id3="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>
</tr>
';
}
$output .= '
<tr>
<td></td>
<td id="Articulo" contenteditable></td>
<td id="fecha" contenteditable></td>
<td id="emailpresta" contenteditable></td>
<td id="emailrecibe" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
</tr>
';
}
else
{
$output .= '<tr>
<td></td>
<td id="Articulo" contenteditable></td>
<td id="fecha" contenteditable></td>
<td id="emailpresta" contenteditable></td>
<td id="emailrecibe" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
</tr>';
}
$output .= '</table>
</div>';
echo $output;
?>
insert.php
<?php
session_start();
$connect = mysqli_connect("############", "############", "", "############");
$sql = "INSERT INTO articulos(Articulo, fecha, emailpresta, emailrecibe) VALUES('".$_POST["Articulo"]."', '".$_POST["fecha"]."'
, '".$_POST["emailpresta"]."', '".$_POST["emailrecibe"]."')";
if(mysqli_query($connect, $sql))
{
echo 'Data Inserted';
}
?>
delete.php
<?php
session_start();
$connect = mysqli_connect("############", "############", "", "############");
$sql = "DELETE FROM articulos WHERE id = '".$_POST["id"]."'";
if(mysqli_query($connect, $sql))
{
echo 'Data Deleted';
}
?>
The problem is likely with you MySQL query. What shows up on screen when you execute the following:
//session_start(); <-- do not start session
$email = '......'; <-- use an email from the DB with data available
$sql = "SELECT * FROM articulos INNER JOIN usuarios
WHERE emailpresta='$email'";
$connect = mysqli_connect(...);
$result = mysqli_query($connect,$sql);
$data = mysqli_fetch_all($result));
print_r($data);
Do you see duplicate rows? If so, you'll need to work on your SQL query. Specifically, you don't tell the database how to join the articulos and usarios tables. Instead of
SELECT * from articulos INNER JOIN usarios WHERE emailpresta='$email'
You probably need something like:
SELECT * from articulos INNER JOIN usarios ON articulos.userID=usarios.ID
WHERE usarios.emailpresta='$email'
Instead of articulos.userID and usarios.ID, use the fields in the two tables that match up. You can learn a bit about INNER JOIN here
Side note
I noticed that you plug $_SESSION['email'] directly into your DB query. If this is a value that you received from the user, you run a huge risk of SQL injection attack, a way for anyone to execute any query (including extracting, modifying or deleting your DB records) using specially crafted inputs. Look into using parameterized queries instead. Even if the contents of $_SESSION['email'] are safe, running raw queries is a risky habit anywhere you take input from the user.
Related
I have a table named tbl_video that consist of two fields (video_id, video_title What I want to add a load more button using Ajax JQuery like social networking sites .I am able to get the first two records but when I try second time nothing happens. I dont get any errors .What am i doing wrong .
this my index.php file I create table structure here and then fetch two records.And get the second record's id value and send it to the more.php file
<body>
<table class="table table-bordered" id="load_data_table">
<thead>
<tr>
<td width="15%">Video Id</td>
<td>Video Title</td>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_array($result)) {
$video_id = $row['video_id']; ?>
<tr>
<td><?php echo $row['video_id']; ?></td>
<td><?php echo $row['video_title']; ?></td>
</tr>
<?php
}
?>
<tr id="remove_row"><td><button type="button" id="more" class="btn btn-success form-control"
data-id="<?php echo $video_id; ?>">More</button></td></tr>
</table>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#more').click(function(){
var id=$(this).data("id");
$.ajax({
url:"more.php",
data:{id:id},
method:"post",
success:function(data){
if(data!=''){
$('#load_data_table').append(data);
$('#remove_row').remove();
}
else{
$('#more').html("No Data");
}
}
and this the more.php file.I think it is self explanatory
$id = $_POST['id'];
$output = '';
$db = mysqli_connect('localhost', 'root', '', 'testing');
$data = mysqli_query($db, "select * from tbl_video where video_id >{$id} limit 2");
if (mysqli_num_rows($data)) {
while ($row = mysqli_fetch_array($data)) {
$output .= '
<tbody>
<tr>
<td>'.$row['video_id'].'</td>
<td>'.$row['video_title'].'</td>
</tr>
';
}
$output .= '
<tr id="remove_row"><td><button type="button" id="more" class="btn btn-success form-control"
data-id="'.$row['video_id'].'">More</button></td></tr></tbody>
';
echo $output;
}
any help would be appriciated.
When you add a new row and there is a new button for More
It has the same id as the original button so there is a conflict there
also the javascript needs to be initiliazed something like this
PHP:
$output .= '<tr id="remove_row"><td><button type="button" class="more btn btn-success form-control" data-id="'.$row['video_id'].'">More</button></td></tr></tbody>';
Javascript:
<script>
function moreButton() {
$('.more').click(function(){
var id=$(this).data("id");
$.ajax({
url:"more.php",
data:{id:id},
method:"post",
success:function(data){
if(data!=''){
$('#load_data_table').append(data);
$('#remove_row').remove();
moreButton();
}
else{
$('.more:last').html("No Data");
}
}
});
}
$(document).ready(function(){
moreButton();
});
Don't forget to remove the id of the more button to a class with .more
So, I'm trying to make a live interactive table that can do active CRUD, but for some reason, the values I'm trying to insert for the add feature are not correctly being read to $_POST.
Here is the table:
<?php
include "../db.php";
$connection = DB::CreateConnection();
$output = '';
$sql = "SELECT * FROM users ORDER BY userid ASC";
$result = mysqli_query($connection, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="10%">Id</th>
<th width="40%">First Name</th>
<th width="40%">Last Name</th>
<th width="10%">Delete</th>
</tr>';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.
$row["userid"].'</td>
<td class="userfirstname" data-id1="'.$row["userid"].'" contenteditable>'.$row["userfirstname"].'</td>
<td class="userlastname" data-id2="'.$row["userid"].'" contenteditable>'.$row["userlastname"].'</td>
<td><button type="button" name="delete_btn" data-id3="'.$row["userid"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>
</tr>';
}
$output .= '
<tr>
<td></td>
<td id="userfirstname" contenteditable></td>
<td id="userlastname" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
</tr>';
}
else
{
$output .= '<tr>
<td colspan="4">Data not Found</td>
</tr>';
}
$output .= '</table>
</div>';
echo $output;
?>
Here it the live data edit file:
<!-- This should be where matt's code goes to edit via popup or live -->
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="table-responsive">
<h3 align-"center">Live Table</h3>
<br />
<div id="live_data"></div>
</div>
</div>
</body>
<script type="text/javascript">
function fetch_data()
{
$.ajax({
url:"UViewTABLE.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
$(document).on('click', '#btn_add', function(){
var userfirstname = $('#userfirstname').text();
var userlastname = $('#userlastname').text();
if(userfirstname == '')
{
alert("Enter First Name");
return false;
}
if(userlastname == '')
{
alert("Enter Last Name");
return false;
}
$.ajax({
url:"UViewTABLEinsert.php",
method:"POST",
data:{userfirstname:userfirstname, userlastname:userlastname},
dataType:"text",
success:function(data)
{
alert(data);
fetch_data();
}
})
});
</script>
Here is the insert file:
<?php
include "../db.php";
$connection = DB::CreateConnection();
$sql = "INSERT INTO users (userfirstname, userlastname) VALUES('".$_POST["userfirstname"]."', '".$_POST["userlastname"]."')";
if(mysqli_query($connection, $sql))
{
echo 'Data Inserted';
}
?>
The error I get is:
Notice: Undefined index: userfirstname in
/var/www/html/UOM/UViewTABLEinsert.php on line 4
Notice: Undefined index: userlastname in
/var/www/html/UOM/UViewTABLEinsert.php on line 4
And, I know that means in my edit file, I'm not defining those indexes properly, but I can't see anything wrong with it. I have researched this problem for 3 days, if someone is kind enough to help, please do, evidently I don't know what I'm doing wrong.
Just remove the dataType from ajax options because you are explicitly setting the data type to text, the data is not submitting in key value pair, and after insertion, send response in JSON, may work. For more about $.ajax's dataType option, refer this. If you want to set dataTyp explicitly, then keep the response in same type as set in ajax request.
I have four different files
index.php
select.php
insert.php
edit.php
delete.php
In my backend I have created a database named 'ecc'
Database 'ecc' has atable named 'task'
The table task has following fields
id, name, category, cost
Datatype for id set to int and index as primary also id field is on auto increment
My Issue:My code is only displaying 'Live Table Data'.
It would be grate if someone rewrite it or suggest some changes.
index.php
<html>
<head>
<title>Live Table Data Edit</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="table-responsive">
<h3 align="center">Live Table Add Edit Delete using Ajax Jquery in PHP Mysql</h3><br />
<div id="live_data"></div>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
function fetch_data()
{
$.ajax({
url:"select.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
$(document).on('click', '#btn_add', function(){
var name = $('#name').text();
var category = $('#category').text();
var cost = $('#cost').text();
if(name == '')
{
alert("Enter Service Name");
return false;
}
if(category == '')
{
alert("Enter Category of Service");
return false;
}
if(cost == '')
{
alert("Enter cost");
return false;
}
$.ajax({
url:"insert.php",
method:"POST",
data:{name:name, category:category, cost:cost},
dataType:"text",
success:function(data)
{
alert(data);
fetch_data();
}
})
});
function edit_data(id, text, column_name)
{
$.ajax({
url:"edit.php",
method:"POST",
data:{id:id, text:text, column_name:column_name},
dataType:"text",
success:function(data){
alert(data);
}
});
}
$(document).on('blur', '.name', function(){
var id = $(this).data("id1");
var name = $(this).text();
edit_data(id, name, "name");
});
$(document).on('blur', '.category', function(){
var id = $(this).data("id2");
var category = $(this).text();
edit_data(id,category, "category");
$(document).on('blur', '.cost', function(){
var id = $(this).data("id3");
var category = $(this).text();
edit_data(id,cost, "cost");
});
$(document).on('click', '.btn_delete', function(){
var id=$(this).data("id4");
if(confirm("Are you sure you want to delete this?"))
{
$.ajax({
url:"delete.php",
method:"POST",
data:{id:id},
dataType:"text",
success:function(data){
alert(data);
fetch_data();
}
});
}
});
});
</script>
select.php
<?php
$connect = mysqli_connect("localhost", "root", "", "ecc");
$output = '';
$sql = "SELECT * FROM task ORDER BY id DESC";
$result = mysqli_query($connect, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="10%">Id</th>
<th width="40%">First Name</th>
<th width="40%">Last Name</th>
<th width="10%">Delete</th>
</tr>';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["id"].'</td>
<td class="name" data-id1="'.$row["id"].'" contenteditable>'.$row["name"].'</td>
<td class="category" data-id2="'.$row["id"].'" contenteditable>'.$row["category"].'</td>
<td class="cost" data-id3="'.$row["id"].'" contenteditable>'.$row["cost"].'</td>
<td><button type="button" name="delete_btn" data-id4="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>
</tr>
';
}
$output .= '
<tr>
<td></td>
<td id="name" contenteditable></td>
<td id="category" contenteditable></td>
<td id="cost" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
</tr>
';
}
else
{
$output .= '<tr>
<td colspan="4">Data not Found</td>
</tr>';
}
$output .= '</table>
</div>';
echo $output;
?>
insert.php
<?php
$connect = mysqli_connect("localhost", "root", "", "test_db");
$sql = "INSERT INTO tbl_sample(name, category, cost) VALUES('".$_POST["name"]."', '".$_POST["category"]."', '".$_POST["cost"]."')";
if(mysqli_query($connect, $sql))
{
echo 'Data Inserted';
}
?>
edit.php
<?php
$connect = mysqli_connect("localhost", "root", "", "ecc");
$id = $_POST["id"];
$text = $_POST["text"];
$column_name = $_POST["column_name"];
$sql = "UPDATE task SET ".$column_name."='".$text."' WHERE id='".$id."'";
if(mysqli_query($connect, $sql))
{
echo 'Data Updated';
}
?>
delete.php
<?php
$connect = mysqli_connect("localhost", "root", "", "ecc");
$sql = "DELETE FROM task WHERE id = '".$_POST["id"]."'";
if(mysqli_query($connect, $sql))
{
echo 'Data Deleted';
}
?>
you can use data tables for this, no need of all this
https://editor.datatables.net/examples/inline-editing/simple
check this, data tables provide libraries for this
I have four different files
index.php
select.php
insert.php
edit.php
delete.php
In my backend I have created a database named 'ecc'
Database 'ecc' has atable named 'task'
The table task has following fields
id, name, category, cost
Datatype for id set to int and index as primary also id field is on auto increment
My issue :( ! ) Notice: Undefined index: id in C:\wamp\www\select.php on line 38
and same for lines 39, 40, 41,
each of the error is displayed twice on the page.
index.php
<html>
<head>
<title>Live Table Data Edit</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="table-responsive">
<h3 align="center">Live Table Add Edit Delete using Ajax Jquery in PHP Mysql</h3><br />
<div id="live_data"></div>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
function fetch_data()
{
$.ajax({
url:"select.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
$(document).on('click', '#btn_add', function(){
var name = $('#name').text();
var category = $('#category').text();
if(name == '')
{
alert("Enter service Name");
return false;
}
if(category == '')
{
alert("Enter category");
return false;
}
$.ajax({
url:"insert.php",
method:"POST",
data:{name:name, category:category},
dataType:"text",
success:function(data)
{
alert(data);
fetch_data();
}
})
});
function edit_data(id, text, column_name)
{
$.ajax({
url:"edit.php",
method:"POST",
data:{id:id, text:text, column_name:column_name},
dataType:"text",
success:function(data){
alert(data);
}
});
}
$(document).on('blur', '.name', function(){
var id = $(this).data("id1");
var name = $(this).text();
edit_data(id, name, "name");
});
$(document).on('blur', '.category', function(){
var id = $(this).data("id2");
var category = $(this).text();
edit_data(id,category, "category");
});
$(document).on('click', '.btn_delete', function(){
var id=$(this).data("id3");
if(confirm("Are you sure you want to delete this?"))
{
$.ajax({
url:"delete.php",
method:"POST",
data:{id:id},
dataType:"text",
success:function(data){
alert(data);
fetch_data();
}
});
}
});
});
</script>
select.php
<?php
$connect = mysqli_connect('localhost', 'root', '','ecc');
mysqli_select_db($connect,'ecc');
if(!$connect){
echo "yes";
}else{
echo "no";
}
$output = '';
$sql = "SELECT name, category FROM addservices ORDER BY id DESC";
$result = mysqli_query($connect, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="10%">Id</th>
<th width="40%">Service Name</th>
<th width="40%">Category</th>
<th width="10%">Delete</th>
</tr>';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["id"].'</td>
<td class="name" data-id1="'.$row["id"].'" contenteditable>'.$row["name"].'</td>
<td class="category" data-id2="'.$row["id"].'" contenteditable>'.$row["category"].'</td>
<td><button type="button" name="delete_btn" data-id3="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>
</tr>
';
}
$output .= '
<tr>
<td></td>
<td id="name" contenteditable></td>
<td id="category" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
</tr>
';
}
else
{
$output .= '<tr>
<td colspan="4">Data not Found</td>
</tr>';
}
$output .= '</table>
</div>';
echo $output;
?>
You are getting this error Undefined index: id
Because you have not put id in select query
$sql = "SELECT id,name, category FROM addservices ORDER BY id DESC";
Chek your query.It need to change as:
$sql = "SELECT * FROM task ORDER BY id DESC";
You are getting this error because you have not selected "id" field in the $sql statement.
You have writen -
$sql = "SELECT name, category FROM addservices ORDER BY id DESC";
It should be
$sql = "SELECT id, name, category FROM addservices ORDER BY id DESC";
I do have a SQLite Web Interface, that should SELECT, INSERT, UPDATE and DELETE data from a SQLite table. Unfortunately, only the SELECT query seems to work. I cannot INSERT, UPDATE or DELETE.
sshTunnel.sqlite --> mydata
CREATE TABLE "mydata" ("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "SSH_USER" VARCHAR, "SSH_IP" VARCHAR, "SSH_PORT" VARCHAR, "SSH_PW" VARCHAR, "SSH_KEYFILE" VARCHAR, "TUNNEL_LOCAL_INTERFACE" VARCHAR, "TUNNEL_LOCAL_PORT" VARCHAR, "TUNNEL_REMOTE_IP" VARCHAR, "TUNNEL_REMOTE_PORT" VARCHAR, "DESCRIPTION" VARCHAR)
Here are my code snippets...
index.php
<script>
$(document).ready(function()
{
function fetch_data()
{
$.ajax({
url:"select.php",
method:"POST",
success:function(data)
{
$('#live_data').html(data);
}
});
}
fetch_data();
$(document).on('click', '#btn_add', function()
{
var SSH_USER = $('#SSH_USER').text();
var SSH_IP = $('#SSH_IP').text();
var SSH_PORT = $('#SSH_PORT').text();
var SSH_PW = $('#SSH_PW').text();
var SSH_KEYFILE = $('#SSH_KEYFILE').text();
var TUNNEL_LOCAL_INTERFACE = $('#TUNNEL_LOCAL_INTERFACE').text();
var TUNNEL_LOCAL_PORT = $('#TUNNEL_LOCAL_PORT').text();
var TUNNEL_REMOTE_IP = $('#TUNNEL_REMOTE_IP').text();
var TUNNEL_REMOTE_PORT = $('#TUNNEL_REMOTE_PORT').text();
var DESCRIPTION = $('#DESCRIPTION').text();
$.ajax({
url:"insert.php",
method:"POST",
data:{SSH_USER:SSH_USER, SSH_IP:SSH_IP, SSH_PORT:SSH_PORT, SSH_PW:SSH_PW, SSH_KEYFILE:SSH_KEYFILE, TUNNEL_LOCAL_INTERFACE:TUNNEL_LOCAL_INTERFACE, TUNNEL_LOCAL_PORT:TUNNEL_LOCAL_PORT, TUNNEL_REMOTE_IP:TUNNEL_REMOTE_IP, TUNNEL_REMOTE_PORT:TUNNEL_REMOTE_PORT, DESCRIPTION:DESCRIPTION},
dataType:"text",
success:function(date)
{
alert(data);
fetch_data();
}
});
});
$(document).on('click', '.btn_edit', function()
{
var SSH_USER = $(this).data("id1");
var SSH_IP = $(this).data("id2");
var SSH_PORT = $(this).data("id3");
var SSH_PW = $(this).data("id4");
var SSH_KEYFILE = $(this).data("id5");
var TUNNEL_LOCAL_INTERFACE = $(this).data("id6");
var TUNNEL_LOCAL_PORT = $(this).data("id7");
var TUNNEL_REMOTE_IP = $(this).data("id8");
var TUNNEL_REMOTE_PORT = $(this).data("id9");
var DESCRIPTION = $(this).data("id10");
if(confirm("Are you sure you want to edit this?"))
{
$.ajax({
url:"edit.php",
method:"POST",
data:{SSH_USER:SSH_USER, SSH_IP:SSH_IP, SSH_PORT:SSH_PORT, SSH_PW:SSH_PW, SSH_KEYFILE:SSH_KEYFILE, TUNNEL_LOCAL_INTERFACE:TUNNEL_LOCAL_INTERFACE, TUNNEL_LOCAL_PORT:TUNNEL_LOCAL_PORT, TUNNEL_REMOTE_IP:TUNNEL_REMOTE_IP, TUNNEL_REMOTE_PORT:TUNNEL_REMOTE_PORT, DESCRIPTION:DESCRIPTION},
dataType:"text",
success:function(date)
{
alert(data);
fetch_data();
}
});
}
});
$(document).on('click', '.btn_delete', function()
{
var id = $(this).data("id12");
if(confirm("Are you sure you want to delete this?"))
{
$.ajax({
url:"delete.php",
method:"POST",
data:{id:id},
dataType:"text",
success:function(date)
{
alert(data);
fetch_data();
}
});
}
});
});
</script>
...
<div id="live_data"></div>
select.php
<?php
$db = new SQLite3('sshTunnel.sqlite');
$results = $db->query('SELECT * FROM mydata');
$output = '';
$output .= '<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th>ID</th>
<th>SSH_USER</th>
<th>SSH_IP</th>
<th>SSH_PORT</th>
<th>SSH_PW</th>
<th>SSH_KEYFILE</th>
<th>TUNNEL_LOCAL_INTERFACE</th>
<th>TUNNEL_LOCAL_PORT</th>
<th>TUNNEL_REMOTE_IP</th>
<th>TUNNEL_REMOTE_PORT</th>
<th>DESCRIPTION</th>
<th>Editieren</th>
<th>Löschen</th>
</tr>';
while ($row = $results->fetchArray())
{
$output .= '<tr>
<td>'.$row["ID"].'</td>
<td class="SSH_USER" data-id1="'.$row["ID"].'" contenteditable="true">'.$row["SSH_USER"].'</td>
<td class="SSH_IP" data-id2="'.$row["ID"].'" contenteditable="true">'.$row["SSH_IP"].'</td>
<td class="SSH_PORT" data-id3="'.$row["ID"].'" contenteditable="true">'.$row["SSH_PORT"].'</td>
<td class="SSH_PW" data-id4="'.$row["ID"].'" contenteditable="true">'.$row["SSH_PW"].'</td>
<td class="SSH_KEYFILE" data-id5="'.$row["ID"].'" contenteditable="true">'.$row["SSH_KEYFILE"].'</td>
<td class="TUNNEL_LOCAL_INTERFACE" data-id6="'.$row["ID"].'" contenteditable="true">'.$row["TUNNEL_LOCAL_INTERFACE"].'</td>
<td class="TUNNEL_LOCAL_PORT" data-id7="'.$row["ID"].'" contenteditable="true">'.$row["TUNNEL_LOCAL_PORT"].'</td>
<td class="TUNNEL_REMOTE_IP" data-id8="'.$row["ID"].'" contenteditable="true">'.$row["TUNNEL_REMOTE_IP"].'</td>
<td class="TUNNEL_REMOTE_PORT" data-id9="'.$row["ID"].'" contenteditable="true">'.$row["TUNNEL_REMOTE_PORT"].'</td>
<td class="DESCRIPTION" data-id10="'.$row["ID"].'" contenteditable="true">'.$row["DESCRIPTION"].'</td>
<td><button type="button" name="edit_btn" data-id11="'.$row["ID"].'" class="btn btn-xs btn-warning btn-block btn_edit">Editieren</button></td>
<td><button type="button" name="delete_btn" data-id12="'.$row["ID"].'" class="btn btn-xs btn-danger btn-block btn_delete">Löschen</button></td>
</tr>';
}
$output .= '<tr>
<td></td>
<td id="SSH_USER" contenteditable="true"</td>
<td id="SSH_IP" contenteditable="true"</td>
<td id="SSH_PORT" contenteditable="true"</td>
<td id="SSH_PW" contenteditable="true"</td>
<td id="SSH_KEYFILE" contenteditable="true"</td>
<td id="TUNNEL_LOCAL_INTERFACE" contenteditable="true"</td>
<td id="TUNNEL_LOCAL_PORT" contenteditable="true"</td>
<td id="TUNNEL_REMOTE_IP" contenteditable="true"</td>
<td id="TUNNEL_REMOTE_PORT" contenteditable="true"</td>
<td id="DESCRIPTION" contenteditable="true"</td>
<td colspan="13"><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-block btn-success">Hinzufügen</button></td>
</tr>
</table>
</div>';
echo $output;
$db->close();
?>
insert.php
<?php
$db = new SQLite3('sshTunnel.sqlite');
$db->exec('INSERT INTO mydata( SSH_USER,
SSH_IP,
SSH_PORT,
SSH_PW,
SSH_KEYFILE,
TUNNEL_LOCAL_INTERFACE,
TUNNEL_LOCAL_PORT,
TUNNEL_REMOTE_IP,
TUNNEL_REMOTE_PORT,
DESCRIPTION)
VALUES( '".$_POST["SSH_USER"]."',
'".$_POST["SSH_IP"]."',
'".$_POST["SSH_PORT"]."',
'".$_POST["SSH_PW"]."',
'".$_POST["SSH_KEYFILE"]."',
'".$_POST["TUNNEL_LOCAL_INTERFACE"]."',
'".$_POST["TUNNEL_LOCAL_INTERFACE"]."',
'".$_POST["TUNNEL_LOCAL_PORT"]."',
'".$_POST["TUNNEL_REMOTE_IP"]."',
'".$_POST["TUNNEL_REMOTE_PORT"]."',
'".$_POST["DESCRIPTION"]."')');
$db->close();
?>
edit.php
<?php
$db = new SQLite3('sshTunnel.sqlite');
$db->exec("UPDATE mydataSET SSH_USER='".$_POST["SSH_USER"]."',
SSH_IP='".$_POST["SSH_IP"]."',
SSH_PORT='".$_POST["SSH_PORT"]."',
SSH_PW='".$_POST["SSH_PW"]."',
SSH_KEYFILE='".$_POST["SSH_KEYFILE"]."',
TUNNEL_LOCAL_INTERFACE='".$_POST["TUNNEL_LOCAL_INTERFACE"]."',
TUNNEL_LOCAL_PORT='".$_POST["TUNNEL_LOCAL_PORT"]."',
TUNNEL_REMOTE_IP='".$_POST["TUNNEL_REMOTE_IP"]."',
TUNNEL_REMOTE_PORT='".$_POST["TUNNEL_REMOTE_PORT"]."',
DESCRIPTION='".$_POST["DESCRIPTION"]."'
WHERE ID='".$_POST["ID"]."'");
$db->close();
?>
delete.php
<?php
$db = new SQLite3('sshTunnel.sqlite');
$db->exec('DELETE FROM mydata WHERE ID='".$_POST["ID"]."'');
$db->close();
?>
Are you sure your scripts are compiling and running?
I spot a strange use of quotes in insert.php: you open the string with single quote and them use double quote to terminate and concatenate... same for delete.php.
Also, make sure you "addslashes" to all the data you place in the query. If a single quote enters any content, all your queries will fail and, of course, security matters.
--
Sérgio