php – 从mysql切换到mysqli后,Ajax的更新语句不能正常工作,但没有错误显示

参见英文答案 > When to use single quotes, double quotes, and back ticks in MySQL                                    12个
我有一个代码,与MysqL工作正常,但已切换到MysqLi它不再运行.数据在选择按钮的表格中正确显示,它看起来像我得到保存gif(因为正在调用Ajax),但是更新语句不会更新数据库.

似乎没有工作的部分是saveedit.PHP

<?PHP
require_once("dbcontroller.PHP");
$db_handle = new DBController();
$column=$_POST['column'];
$value=$_POST['value'];
$id=$_POST['id'];
$sql = "UPDATE PHP_interview_questions SET `$column` = '$value' WHERE id=$id)";
$result = MysqLi_query ($conn, $sql) or die(MysqLi_error ($dbc));
?>

主页是

<?PHP
require_once("dbcontroller.PHP");
$db_handle = new DBController();
$sql = "SELECT * from PHP_interview_questions";
$faq = $db_handle->runQuery($sql);
?>
<html>
    <head>
      <title>PHP MysqL Inline Editing using jQuery Ajax</title>

        <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
        <script>
        function showEdit(editableObj) {
            $(editableObj).css("background","#FFF");
        } 

        function savetoDatabase(editableObj,column,id) {
            $(editableObj).css("background","#FFF url(loaderIcon.gif) no-repeat right");
            $.ajax({
                url: "saveedit.PHP",
                type: "POST",
                data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
                success: function(data){
                    $(editableObj).css("background","#FDFDFD");
                }        
           });
        }
        </script>
    </head>
    <body>      
       <table class="tbl-qa">
          <thead>
              <tr>
                <th class="table-header" width="10%">Q.No.</th>
                <th class="table-header">Question</th>
                <th class="table-header">Answer</th>
              </tr>
          </thead>
          <tbody>
          <?PHP
          foreach($faq as $k=>$v) {
          ?>
              <tr class="table-row">
                <td><?PHP echo $k+1; ?></td>
                <td contenteditable="true" onBlur="savetoDatabase(this,'question','<?PHP echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?PHP echo $faq[$k]["question"]; ?></td>
                <td contenteditable="true" onBlur="savetoDatabase(this,'answer','<?PHP echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?PHP echo $faq[$k]["answer"]; ?></td>
              </tr>
        <?PHP
        }
        ?>
          </tbody>
        </table>
    </body>
</html>

使用db_controller.PHP连接到数据库并处理结果集创建(数据库连接详细信息位于此处未发布的文件中):

function __construct() {
        $conn = $this->connectDB();
        if(!empty($conn)) {
            $this->selectDB($conn);
        }
    }

    function connectDB() {
        $conn = MysqLi_connect($this->host,$this->user,$this->password);
        return $conn;
    }

    function selectDB($conn) {
        MysqLi_select_db($conn, $this->database);
    }

    function runQuery($query) {
        $conn = MysqLi_connect($this->host,$this->user,$this->password);
        MysqLi_select_db($conn, $this->database);
        $result = MysqLi_query($conn, $query)or die(MysqLi_error($conn));
        while($row=MysqLi_fetch_assoc($result)) {
            $resultset[] = $row;
        }       
        if(!empty($resultset))
            return $resultset;
    }

    function numRows($query) {
        $result  = MysqLi_query($conn, $query);
        $rowcount = MysqLi_num_rows($result);
        return $rowcount;   
    }
}
?>

我现在已经将dbcontroller更改为以下内容以简化并尝试抛出错误但仍然没有得到任何结果

<?PHP
class DBController {
    private $host = "***********";
    private $user = "***********";
    private $password = "**********";
    private $database = "************";



    function __construct() {
        $conn = MysqLi_connect($this->host,$this->user,$this->password,$this->database) OR die (MysqLi_connect_error());
    }

    function runQuery($query) {
        $conn = MysqLi_connect($this->host,$this->user,$this->password);
        MysqLi_select_db($conn, $this->database);
        $result = MysqLi_query($conn, $query)or die(MysqLi_error($conn));
        while($row=MysqLi_fetch_assoc($result)) {
            $resultset[] = $row;
        }       
        if(!empty($resultset))
            return $resultset;
    }

    function numRows($query) {
        $result  = MysqLi_query($conn, $query);
        $rowcount = MysqLi_num_rows($result);
        return $rowcount;   
    }
}
?

&GT

解决方法:

删除列周围的单个qoutes名称

$sql = "UPDATE PHP_interview_questions SET $column = '$value' WHERE id=$id)";

或者,如果列中有特殊字符,请使用反引号来逃避它们.

$sql = "UPDATE PHP_interview_questions SET `$column` = '$value' WHERE id=$id)";

如果id是一个数值,你也不需要单个qoutes.

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...