duanmu5039 2019-02-05 04:58
浏览 42

jTables无法添加,更新和删除

I am practicing to use jTables Php Sample and changing it to mysqli. The data loaded successfully in the table but when I add, update and delete I got error: "An error occured while communicating to the server." error message PersonActionsPagedSorted.php

<?php

try
{
    //Open database connection

    /* Database connection start */
    $con=mysqli_connect("localhost","root","");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }


    mysqli_select_db($con,"jtabletestdb");




    //Getting records (listAction)
    if(isset($_GET["action"]) || "list")
    {
        //Get record count
        $sql = "SELECT COUNT(PersonId) AS RecordCount FROM people;";
        $result = mysqli_query($con, $sql) or die("error to fetch employees data");

        $row = mysqli_fetch_assoc($result);
        $recordCount = $row['RecordCount'];

        //Get records from database
        $sql = "SELECT * FROM people ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";";


        $result = mysqli_query($con, $sql)or die("error to sort table data");
        //Add all records to an array
        $rows = array();
        while($row = mysqli_fetch_array($result))
        {
            $rows[] = $row;
        }

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $recordCount;
        $jTableResult['Records'] = $rows;

        print json_encode($jTableResult);
    }

    //Creating a new record (createAction)
    else if(isset($_GET["action"]) || "create")
    {
        //Insert record into database
        $sql = "INSERT INTO people(Name, Age, RecordDate) VALUES('" . $_POST["Name"] . "', '" . $_POST["Age"] . "',now());";

        $result = mysqli_query($con, $sql);

        //Get last inserted record (to return to jTable)
        $sql = "SELECT * FROM people WHERE PersonId = LAST_INSERT_ID();";

        $result = mysqli_query($con, $sql) or die("error to insert new data");

        $row = mysqli_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(isset($_GET["action"]) || "update")
    {
        //Update record in database
        $result = mysqli_query("UPDATE people SET Name = '" . $_POST["Name"] . "', Age = " . $_POST["Age"] . " WHERE PersonId = " . $_POST["PersonId"] . ";");

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        print json_encode($jTableResult);
    }

    //Deleting a record (deleteAction)
    else if(isset($_GET["action"]) || "delete")
    {
        //Delete from database
        $sql = "DELETE FROM people WHERE PersonId = " . $_POST["PersonId"] . ";";
        $result = mysqli_query($con, $sql) or die("error to sort table data");

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        print json_encode($jTableResult);
    }

    //Close database connection
    mysqli_close($con);

}
catch(Exception $ex)
{
    //Return error message
    $jTableResult = array();
    $jTableResult['Result'] = "ERROR";
    $jTableResult['Message'] = $ex->getMessage();
    print json_encode($jTableResult);
}

?>

jTableSimplePagedSorted.php

<html>
  <head>

    <link href="themes/redmond/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" />
    <link href="Scripts/jtable/themes/metro/green/jtable.css" rel="stylesheet" type="text/css" />

    <script src="scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script src="scripts/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
    <script src="Scripts/jtable/jquery.jtable.js" type="text/javascript"></script>

  </head>
  <body>
    <div id="PeopleTableContainer" style="width: 600px;"></div>
    <script type="text/javascript">

        $(document).ready(function () {

            //Prepare jTable
            $('#PeopleTableContainer').jtable({
                title: 'Table of people',
                paging: true,
                pageSize: 10,
                sorting: true,
                defaultSorting: 'Name ASC',
                actions: {
                    listAction: 'PersonActionsPagedSorted.php?action=list',
                    createAction: 'PersonActionsPagedSorted.php?action=create',
                    updateAction: 'PersonActionsPagedSorted.php?action=update',
                    deleteAction: 'PersonActionsPagedSorted.php?action=delete'
                },
                fields: {
                    PersonId: {
                        key: true,
                        create: false,
                        edit: false,
                        list: false
                    },
                    Name: {
                        title: 'Author Name',
                        width: '40%'
                    },
                    Age: {
                        title: 'Age',
                        width: '20%'
                    },
                    RecordDate: {
                        title: 'Record date',
                        width: '30%',
                        type: 'date',
                        create: false,
                        edit: false
                    }
                }
            });

            //Load person list from server
            $('#PeopleTableContainer').jtable('load');

        });

    </script>

  </body>
</html>

I got error message and I dont know where it went wrong. When I checked I cant debug. There is also no error in when I run the developer tools in chrome.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?