gpt4 book ai didi

javascript - 编辑后无法将数据保存到数据库中,但网格显示更改

转载 作者:行者123 更新时间:2023-11-30 00:55:07 24 4
gpt4 key购买 nike

当我编辑网格中的行时,更改仅显示在该网格中。但是当我重新加载网格时,所有更新都会消失,因为数据库中的数据尚未更新

谁能提示一下我哪里写错了。这是我的 test.html

<script type="text/javascript" src="js/jquery.js"></script>

<script src="js/i18n/grid.locale-ru.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="js/jquery.jqgrid.min.js" type="text/javascript"></script>
<script src="js/grid.common.js" type="text/javascript"></script>
<script src="js/grid.formedit.js" type="text/javascript"></script>

<script type="text/javascript">
$(function(){
$('#table').jqGrid({
url:'test.php',
datatype: 'json',
mtype: 'POST',
height: 500,
colNames:['Код страны','Код региона', 'Город','Долгота','Широта','nbip'],
colModel :[
{name:'country_code', index:'country_code', width:80, editable:true, edittype:'text'},
{name:'region_code', index:'region_code', width:180, editable:true, edittype:'text'},
{name:'city', index:'city', width:190, editable:true, edittype:'text'},
{name:'latitude', index:'latitude', width:160, editable:true, edittype:'text'},
{name:'longitude', index:'longitude', width:160, editable:true, edittype:'text'},
{name:'nbip', index:'nbip', width:130, editable:true, edittype:'text'}],

pager: $('#tablePager'),
pgtext : "Page {0} of {1}",
rowNum:100,
rowList:[10,20,30,100],
sortname: 'city',
sortorder: 'asc',
caption: 'Загрузка JSON данных',
rownumbers: true,
rownumWidth: 40

});




jQuery("#table").jqGrid('navGrid','#tablePager',
{edit:true,add:true,del:true,search:true}, //options
{url:'edit.php', height:280,reloadAfterSubmit:false, closeAfterEdit: true}, // edit options
{height:280,reloadAfterSubmit:false}, // add options
{reloadAfterSubmit:false}, // del options
{} // search options
);


}



});
</script>

</head>
<body>
<table id="table"></table>
<div id="tablePager"></div>

</form>
</body>
</html>

测试.php

    <?php

$db = mysql_connect($db_host, $db_login, $db_passwd);
mysql_select_db($db_name);

$page = $_POST['page'];
$limit = $_POST['rows'];
$sidx = $_POST['sidx'];
$sord = $_POST['sord'];

if(!$sidx) $sidx =1;

$result = mysql_query("SELECT COUNT(*) AS count FROM cities");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];


if( $count > 0 && $limit > 0) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}

if ($page > $total_pages) $page=$total_pages;

$start = $limit*$page - $limit;
if($start <0) $start = 0;

$query = "SELECT id, country_code, region_code, city, latitude, longitude, nbip FROM cities ORDER BY ".$sidx." ".$sord." LIMIT ".$start.", ".$limit;
$result = mysql_query($query);

$data->page = $page;
$data->total = $total_pages;
$data->records = $count;

$i = 0;
while($row = mysql_fetch_assoc($result)) {
$data->rows[$i]['id'] = $row[id];
$data->rows[$i]['cell'] = array($row[country_code],$row[region_code],$row[city],$row[latitude],$row[longitude],$row[nbip]);
$i++;
}

header("Content-type: text/script;charset=utf-8");

echo json_encode($data);
?>

编辑.php

$db = mysql_connect($db_host, $db_login, $db_passwd) or die("Connection Error: " . mysql_error());
mysql_select_db($db_name) or die("Error conecting to db.");
//mysql_query("SET NAMES 'utf8'");

// And so on for the other form values
$id = $_POST['id'];
$country_code = mysql_real_escape_string($_POST['country_code']);
$region_code = mysql_real_escape_string($_POST['region_code']);
$city = $_POST['city'];
$latitude = mysql_real_escape_string($_POST['latitude']);
$longitude = mysql_real_escape_string($_POST['longitude']);
$nbip = mysql_real_escape_string($_POST['nbip']);

if($_POST['oper']=='add'){
// mysql insert
}else if($_POST['oper']=='edit'){
// mysql update
$query = "UPDATE cities SET city = '".$city."' WHERE id = ".$id;
$result = mysqli_query($db,$query) or die(mysql_error());
break;
}else if($_POST['oper']=='del'){
// mysql delete
}

?>

谢谢。

最佳答案

您正在使用mysqli进行数据更新,并在代码中使用mysql_real_escape_string、mysql_connect、mysql_select_db

更新您的代码以仅使用mysqli,而不是同时使用mysqlmysqli

关于javascript - 编辑后无法将数据保存到数据库中,但网格显示更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20638150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com