Dzień dobry,
Znalazłem w sieci skrypt dynamicznego edytowania bazy mysql (https://webdamn.com/create-editable-bootstrap-table-with-php-mysql/).
Przerabiam go pod siebie i tak jak kod PHP rozumiem tak JS nie za koniecznie. Próbowałem logicznie zmienić wartości, ale nie rozumiem schematu działania funkcji edycji w tym pliki: editable.js
Konkretnie (moja wersja) :
$( document ).ready(function() {
$('#editableTable').SetEditable({
columnsEd: "0,1,2,3",
onEdit: function(columnsEd) {
// console.log("===edit=="+(this));
var empId = columnsEd[0].childNodes[1].innerHTML;
var imie = columnsEd[0].childNodes[3].innerHTML;
var nazwisko = columnsEd[0].childNodes[5].innerHTML;
var email = columnsEd[0].childNodes[7].innerHTML;
$.ajax({
type: 'POST',
url : "action.php",
dataType: "json",
data: {id:empId, imie:imie, nazwisko:nazwisko, email:email, action:'edit'},
success: function (response) {
if(response.status) {
// show update message
}
}
});
},
onBeforeDelete: function(columnsEd) {
var empId = columnsEd[0].childNodes[1].innerHTML;
$.ajax({
type: 'POST',
url : "action.php",
dataType: "json",
data: {id:empId, action:'delete'},
success: function (response) {
if(response.status) {
// show delete message
}
}
});
},
});
});
Edytuje mi tylko kolumnę imie, nazwisko i email nie chce. Wyświetla wszystko poprawnie
Moja baza wygląda tak :
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`imie` text NOT NULL,
`nazwisko` text NOT NULL,
`email` text NOT NULL,
`password` varchar(255) NOT NULL,
`telefon` text NOT NULL,
`ip` text NOT NULL,
`mac` text NOT NULL,
`cookie` text NOT NULL,
`pcname` text NOT NULL,
`os` text NOT NULL,
`data` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Plik action.php:
<?php
include_once("../../config.php");
if ($_POST['action'] == 'edit' && $_POST['id']) {
$updateField='';
if(isset($_POST['imie'])) { $updateField.= "imie='".$_POST['imie']."'";
} else if(isset($_POST['nazwisko'])) { $updateField.= "nazwisko='".$_POST['nazwisko']."'";
} else if(isset($_POST['email'])) { $updateField.= "email='".$_POST['email']."'";
}
if($updateField && $_POST['id']) {
$sqlQuery = "UPDATE users SET $updateField WHERE id='" . $_POST['id'] . "'";
mysqli_query
($db1, $sqlQuery) or
die("database error:". mysqli_error
($db1)); "message" => "Record Updated",
"status" => 1
);
}
}
if ($_POST['action'] == 'delete' && $_POST['id']) {
$sqlQuery = "DELETE FROM users WHERE id='" . $_POST['id'] . "'";
mysqli_query
($db1, $sqlQuery) or
die("database error:". mysqli_error
($db1)); "message" => "Record Deleted",
"status" => 1
);
}
?>
Oraz index.php
<?php
include("inc/header.php");
?>
<title>webdamn.com : Demo Create Editable Bootstrap Table with jQuery, PHP & MySQL</title>
<?php include('inc/container.php');?>
<style>
table {
background-color: #181818;
}
table, .table {
color: #fff;
}
</style>
<div class="container">
<div class="row">
<h2>Example: Create Editable Bootstrap Table with jQuery, PHP & MySQL</h2>
<?php
include_once("../../config.php");
$sqlQuery = "SELECT id, imie, nazwisko, email FROM users LIMIT 5";
$resultSet = mysqli_query
($db1, $sqlQuery) or
die("database error:". mysqli_error
($db1)); ?>
<table id="editableTable" class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Imie</th>
<th>Nazwisko</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php while( $developer = mysqli_fetch_assoc($resultSet) ) { ?>
<tr id="
<?php echo $developer ['id']; ?>">
<td>
<?php echo $developer ['id']; ?></td>
<td>
<?php echo $developer ['imie']; ?></td>
<td>
<?php echo $developer ['nazwisko']; ?></td>
<td>
<?php echo $developer ['email']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<script src="plugin/bootstable.js"></script>
<script src="js/editable.js"></script>
<?php include('inc/footer.php');?>