php 增删改操作

来源:转载

最近在慕课网上学习php + ajax 实现表格的实时编辑,老师讲的非常好,感谢老师,按着老师的讲解,自己敲了一遍,收货很多。






可编辑表格DEMO























1 2 3 4 5 6 7 8 增加





$(function() {
var tbody = $("table.data tbody");
var init_data_url = "data.php?action=init_data_list";
$.get(init_data_url, function(data) {
//打断点后可以在console 里面写data 回车显示data数据
//debugger;
//从后台获取所有的数据并用$.parseJSON转换成对象
var row_items = $.parseJSON(data);
for (i = 0; i < row_items.length; i++) {
var data_dom = create_row(row_items[i]);
tbody.append(data_dom);
}
})
function delHandler() {
var data_id = $(this).attr("dataid");
var meButton = $(this);
$.post("data.php?action=del_row", { dataid: data_id }, function(res) {
alert(res);
console.log(res == "ok");
if (res == "ok") {
$(meButton).parent().parent().remove();
} else {
alert("删除失败...");
}
});
}
function editHander() {
alert("123");
var data_id = $(this).attr("dataid");
var meButton = $(this);
var meRow = $(this).parent().parent();
var editRow = $("");
debugger;
for (var i = 0; i < 8; i++) {
var editTd = $("");
var v = meRow.find("td:" + 'eq(' + i + ')').html();
editTd.find("input").val(v);
editRow.append(editTd);
}
var opt_td = $("");
var saveButton = $("保存 ");
saveButton.click(function() {
var currentRow = $(this).parent().parent();
var input_fields = currentRow.find("input");
var post_fields = {};
for (var i = 0; i < input_fields.length; i++) {
post_fields["col_" + i] = input_fields[i].value;
}
post_fields["id"] = data_id;
$.post("data.php?action=edit_row", post_fields, function(res) {
if (res = "ok") {
var newUpdateRow = create_row(post_fields);
currentRow.replaceWith(newUpdateRow);
} else {
alert("数据更新失败...")
}
})
})
var cancelButton = $("取消");
opt_td.append(saveButton);
opt_td.append(cancelButton);
editRow.append(opt_td);
meRow.replaceWith(editRow);
}
function create_row(data_item) {
var row_obj = $("");
for (var k in data_item) {
if (k != "id") {
var col_td = $("");
col_td.html(data_item[k]);
row_obj.append(col_td);
}
}
var delButton = $("删除 ");
delButton.attr("dataid", data_item["id"]);
delButton.click(delHandler);
var editButton = $(" 编辑");
editButton.attr("dataid", data_item["id"]);
editButton.click(editHander);
var opt_td = $("");
opt_td.append(delButton);
opt_td.append(editButton);
row_obj.append(opt_td);
return row_obj;
}
$("#addBtn").click(function() {
var addRow = $("");
for (i = 0; i < 8; i++) {
var col_td = $("");
addRow.append(col_td);
}
var col_opt = $("");
var confirmBtn = $("确认 ");
confirmBtn.click(function() {
var currentRow = $(this).parent().parent();
var input_fields = currentRow.find("input");
var post_fields = {};
for (i = 0; i < input_fields.length; i++) {
post_fields["col_" + i] = input_fields[i].value;
}
$.post("data.php?action=add_row", post_fields, function(res) {
if (res > 0) {
post_fields["id"] = res;
var postAddRow = create_row(post_fields);
currentRow.replaceWith(postAddRow);
} else {
alert("插入失败...")
}
})
})
var cancelBtn = $("取消 ");
cancelBtn.click(function() {
$(this).parent().parent().remove();
})
col_opt.append(confirmBtn);
col_opt.append(cancelBtn);
addRow.append(col_opt);
tbody.append(addRow);
})
})
<?php
$action = $_GET['action'];
switch ($action) {
case 'init_data_list':
init_data_list();
break;
case 'add_row':
add_row();
break;
case 'del_row':
del_row();
break;
case 'edit_row':
edit_row();
break;
}
function init_data_list() {
$sql = "SELECT * FROM `et_data`";
$query = query_sql($sql);
while ($row = $query->fetch_assoc()) {
$data[] = $row;
}
//把数据转换成json格式
echo json_encode($data);
}
function del_row() {
//echo "ok";
$dataid = $_POST["dataid"];
$sql = "DELETE FROM et_data WHERE id =" . $dataid;
if (query_sql($sql)) {
echo "ok";
} else {
echo "db error...";
}
}
function add_row() {
$sql = 'INSERT INTO et_data (c_a,c_b,c_c,c_d,c_e,c_f,c_g,c_h) VALUES(';
for ($i = 0; $i < 8; $i++) {
$sql.= '/'' . $_POST['col_' . $i] . '/', ';
}
$sql = trim($sql, ", ");
$sql.= ')';
if ($res = query_sql($sql, "SELECT LAST_INSERT_ID() as Ld")) {
$d = $res->fetch_assoc();
echo $d["Ld"];
} else {
echo "db error...";
}
}
function edit_row() {
$sql = "UPDATE et_data SET ";
$id = $_POST["id"];
unset($_POST["id"]);
for ($i = 0; $i < 8; $i++) {
$sql.= ' c_' . chr(97 + $i) . '= /'' . $_POST["col_" . $i] . '/', ';
}
$sql = trim($sql, ", ");
$sql.= "WHEREok";
} else {
echo "db error ...";
}
}
function query_sql() {
$mysqli = new mysqli("127.0.0.1", "root", "123", "demodb");
$sqls = func_get_args();
foreach ($sqls as $s) {
$query = $mysqli->query($s);
}
$mysqli->close();
return $query;
}
?>

分享给朋友:
您可能感兴趣的文章:
随机阅读:
栏目列表
推荐内容