我在我的网站上使用JQuery jTable和PHP。
从数据库加载数据时工作完美!但是当更新数据时:发送POST到 编辑行doesent更新MySQL数据库,只是jtable,但重新加载页面“从mysql加载”后,数据又回到了更新状态。
删除数据工作,只更新不更新
我的js:
//Prepare jTable
$('#log').jtable({
title: 'Domains',
toolbar: {
hoverAnimation: true, //Enable/disable small animation on mouse hover to a toolbar item.
hoverAnimationDuration: 60, //Duration of the hover animation.
hoverAnimationEasing: undefined, //Easing of the hover animation. Uses jQuery's default animation ('swing') if set to undefined.
items: [] //Array of your custom toolbar items.
},
paging: true,
sorting: true,
pageSize : 10,
pageSizes : [ 2, 5, 10, 15, 20, 50, 75, 100, 200, 500 ],
defaultSorting: 'domain ASC',
actions: {
listAction: 'actions.php?action=list',
createAction: 'actions.php?action=create',
updateAction: 'actions.php?action=update',
// deleteAction: 'actions.php?action=delete'
},
messages: DeutschMessages,
fields: {
id_domain: {
key: true,
title: 'ID',
create: false,
edit: false,
list: true
},
domain: {
title: 'Domainname',
width: '30%'
},
exclude: {
title: 'Exclude',
defaultValue: 'www,ns,ftp,mail,mx,pop,smtp',
width: '40%'
},
dnsip: {
title: 'DNS Server',
width: '20%'
},
key: {
title: 'Key',
sorting: false,
list: false,
width: '20%'
},
enable_a: {
title: 'A',
options: ['1','0'],
sorting: false,
width: '20%'
},
enable_ns: {
title: 'NS',
options: ['1','0'],
sorting: false,
width: '20%'
},
enable_url: {
title: 'URL',
options: ['1','0'],
sorting: false,
width: '20%'
},
max: {
title: 'MAX',
defaultValue: '-1',
sorting: false,
width: '20%'
}
}
});
//Load person list from server
$('#log').jtable('load');
});
和php脚本:
//Open database connection
$con = mysql_connect($mysql_host,$mysql_user,$mysql_pass);
mysql_select_db($mysql_db,$con);
//Getting records (listAction)
if($_GET["action"] == "list")
{
if (empty($_POST['search']))
{
$search = NULL;
$result = mysql_query("SELECT COUNT(*) AS RecordCount FROM domains;");
$row = mysql_fetch_array($result);
$recordCount = $row['RecordCount'];
$result = mysql_query("SELECT * FROM domains ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";");
}
else
{
$search = mysql_real_escape_string($_POST['search']);
$result = mysql_query("SELECT COUNT(*) AS RecordCount FROM domains WHERE id_domain LIKE '%".$search."%' or domain LIKE '%".$search."%' or exclude LIKE '%".$search."%' or dnsip LIKE '%".$search."%';");
$row = mysql_fetch_array($result);
$recordCount = $row['RecordCount'];
//Get records from database
$result = mysql_query("SELECT * FROM domains WHERE id_domain LIKE '%".$search."%' or domain LIKE '%".$search."%' or exclude LIKE '%".$search."%' or dnsip LIKE '%".$search."%' ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";");
$_SESSION["query"] = "SELECT * FROM domains WHERE id_domain LIKE '%".$search."%' or domain LIKE '%".$search."%' or exclude LIKE '%".$search."%' or dnsip LIKE '%".$search."%' ORDER BY " . $_GET["jtSorting"];
$_SESSION["contador"] = "SELECT COUNT(*) AS RecordCount FROM domains WHERE id_domain LIKE '%".$search."%' or domain LIKE '%".$search."%' or exclude LIKE '%".$search."%' or dnsip LIKE '%".$search."%'";
}
//Add all records to an array
$rows = array();
while($row = mysql_fetch_array($result))
{
$rows[] = $row;
}
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['Records'] = $rows;
$jTableResult['TotalRecordCount'] = $recordCount;
print json_encode($jTableResult);
}
//Creating a new record (createAction)
else if($_GET["action"] == "create")
{
//Insert record into database
$result = mysql_query("INSERT INTO `domains`(`id_domain`, `domain`, `exclude`, `dnsip`, `key`, `enable_a`, `enable_ns`, `enable_url`, `max`) VALUES ('', '".$_POST["domain"]."','".$_POST["exclude"]."','".$_POST["dnsip"]."','".$_POST["key"]."','".$_POST["enable_a"]."','".$_POST["enable_ns"]."','".$_POST["enable_url"]."','".$_POST["max"]."');");
//Get last inserted record (to return to jTable)
$result = mysql_query("SELECT * FROM domains WHERE id_domain = last_insert_id();"); // WHERE id_domain = last_insert_id();"); // id_domain = LAST_INSERT_ID();");
$row = mysql_fetch_array($result);
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['Record'] = $row;
print json_encode($jTableResult);
}
//Updating a record (updateAction)
else if($_GET["action"] == "update")
{
$id = $_REQUEST['id_domain'];
//Update record in database
$result = mysql_query("UPDATE domains SET domain = '" . addslashes($_POST["domain"]) . "', exclude = '" . addslashes($_POST["exclude"]) . "', dnsip = '" . addslashes($_POST["dnsip"]) . "', key = '" . addslashes($_POST["key"]) . "', enable_a = '" . addslashes($_POST["enable_a"]) . "', enable_ns = '" . addslashes($_POST["enable_ns"]) . "', enable_url = '" . addslashes($_POST["enable_url"]) . "', max = '" . addslashes($_POST["max"]) . "' WHERE id_domain = $id;");
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
print json_encode($jTableResult);
}
else if($_GET["action"] == "listname")
...
点击更新后:
回应总是“好”
我无法发现错误./
任何帮助?
答案 0 :(得分:0)
你见过这个吗?
updateAction: 'actions.php?action=update',
// deleteAction: 'actions.php?action=delete'
应该是:
updateAction: 'actions.php?action=update'
// deleteAction: 'actions.php?action=delete'
第一行末尾没有逗号(你删除了下一行,所以这是最后一行。
此外:您应该考虑切换到mysql安全站点的PDO。
最后但并非最不重要:您没有处理插入查询中的错误。尝试类似:
if(mysql_query("INSERT INTO `domains`
(`id_domain`, `domain`, `exclude`, `dnsip`, `key`, `enable_a`, `enable_ns`, `enable_url`, `max`)
VALUES
('','".$_POST["domain"]."','".$_POST["exclude"]."','".$_POST["dnsip"]."','".$_POST["key"]."','".$_POST["enable_a"]."','".$_POST["enable_ns"]."','".$_POST["enable_url"]."','".$_POST["max"]."');")){ $jTableResult['Result'] = "OK";
}else{
$jTableResult['Result'] = "KO";
}