jtable正确地从mysql中提取数据,插入新记录工作,更新/删除卡住

时间:2014-04-27 16:01:35

标签: php jquery mysql jtable

当谈到PHP和jQuery时,我是新手,所以我正在练习并且我遇到了jtable问题。 我能够正确地从MySQL数据库中提取数据。插入新记录是有效的,但我坚持编辑/删除部分。它接受命令,将结果返回OK,在浏览器中显示,但不在数据库中处理。我无法弄清楚我做错了什么。有什么猜测?

<?php

try
{
    //Open database connection
    $con = mysql_connect("localhost","root","");
    mysql_select_db("lab", $con);
    mysql_query('SET NAMES utf8 COLLATE utf8_general_ci');

    //Getting records (listAction)
    if($_GET["action"] == "list")
    {
        //Get record count
        $result = mysql_query("SELECT COUNT(*) AS RecordCount FROM lab;");
        $row = mysql_fetch_array($result);
        $recordCount = $row['RecordCount'];

        //Get records from database
        $result = mysql_query("SELECT * FROM lab ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";");

        //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['TotalRecordCount'] = $recordCount;
        $jTableResult['Records'] = $rows;
        print json_encode($jTableResult);
    }
    //Creating a new record (createAction)
    else if($_GET["action"] == "create")
    {
        //Insert record into database
        $result = mysql_query("INSERT INTO lab (mikroorganizam) VALUES('" . $_POST["mikroorganizam"] . "');");

        //Get last inserted record (to return to jTable)
        $result = mysql_query("SELECT * FROM lab WHERE id = 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")
    {
        //Update record in database
        $result = mysql_query("UPDATE lab SET mikroorganizam = " . $_POST["mikroorganizam"] . " WHERE id = " . $_POST["id"] . ";");

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        print json_encode($jTableResult);
    }
    //Deleting a record (deleteAction)
    else if($_GET["action"] == "delete")
    {
        //Delete from database
        $result = mysql_query("DELETE FROM lab WHERE id = " . $_POST["id"] . ";");

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

    //Close database connection
    mysql_close($con);

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

?>

0 个答案:

没有答案