jTables无法添加,更新和删除

时间:2019-02-05 04:58:53

标签: php ajax pdo jtable

我正在练习使用jTables Php Sample并将其更改为mysqli。数据已成功加载到表中,但是在添加,更新和删除时出现错误:“与服务器通信时发生错误。” 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>

我收到错误消息,但我不知道哪里出错了。当我检查我不能调试。在chrome中运行开发人员工具时也没有错误。

0 个答案:

没有答案