shuangji.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" id="text" style="display: none;position: absolute;top: 0;left: 0;">
<table border="1">
<tr>
<th>ID</th>
<th>用户</th>
<th>密码</th>
</tr>
<?php
mysql_connect("localhost","root","");
mysql_select_db("0503");
mysql_query("set names utf8");
$sql = "SELECT * FROM yonghu";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
foreach ($row as $value){
echo "<td>{$value}</td>";
}
echo "</tr>";
}
?>
</table>
<script src="jquery-2.1.3.min.js"></script>
<script>
var that = null;
$("td").on("dblclick",function () {
that = $(this);
$("#text").show().width($(this).width()).height($(this).height()).css({
top: that.offset().top,
left: that.offset().left
}).focus().val(that.html());
})
var arr = ["id","user","pass"];
$("#text").on("keydown",function (event) {
var targetID = that.parent("tr").children().first().html();
if (event.keyCode == 13){
$.ajax({
url:"shuang_api.php",
type:"get",
data:{
id:targetID,
// index() 表示在同辈元素中的下标
type:arr[that.index()],
value:$("#text").val()
},
success:function (data) {
// console.log(data);
var dataObj = JSON.parse(data);
if (dataObj.err == 0){
that.html($("#text").val());
$("#text").hide();
}else{
alert(dataObj.msg);
}
}
})
}
})
</script>
</body>
</html>
common.php
<?php
/**
* Created by PhpStorm.
* User: dllo
* Date: 16/8/16
* Time: 上午9:23
*/
$mysql = mysql_connect("localhost","root","");
if (!$mysql){
echo '{"err":1,"msg":"数据库连接失败"}';
// 程序直接退出
exit();
}
$db = mysql_select_db("0503");
if (!$db){
echo '{"err":1,"msg":"0503打开失败"}';
exit();
}
mysql_query("set names uft8");
shuang_api.php
<?php
/**
* Created by PhpStorm.
* User: dllo
* Date: 16/8/16
* Time: 上午9:14
*/
//api:shuang_api.php?type=xx&value=xx&id=xx;
// 参数: type,表示要修改的字段名
// value:修改后的值
// id:被修改的id
// 返回值:{"err":0,"msg":"修改成功"}
require_once "common.php";
// 要修改的字段名
$type = $_GET["type"];
$value = $_GET["value"];
$id = $_GET["id"];
$sql = "UPDATE yonghu SET {$type} = '{$value}' WHERE id = '{$id}'";
mysql_query($sql);
if (mysql_affected_rows() > 0){
echo '{"err":0 ,"msg":"修改成功"}';
}else{
echo '{"err":1 ,"msg":"修改失败"}';
}