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

标签 javascript php mysql

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

谁能提示一下我哪里写错了。 这是我的 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/

相关文章:

javascript - 如何将滚动添加到 react-bootstrap Modal.Body

php - 将可变数量的变量传递给 PHP 中的类

php - 将标签添加到数据库值

javascript - $ 在 JavaScript 中是什么意思?如何在 Win8 应用程序中替换它?

javascript - 将生成的字符串作为 javascript 函数中的变量传递

javascript - Facebook API 错误 100 - 无效链接

php - 必填字段缺少 json/android

javascript - PHP/AJAX 调用 Python - 403 禁止

php - 查询在 Mysql 数据库中不起作用

转储导入后,Mysql 卡住了数百个 sleep 进程