更新查询的SQL语法错误

时间:2017-02-22 09:27:18

标签: php mysql

我正在尝试更新数据库中的表。是什么让这个奇怪的是它实际更新,但我得到一个错误

代码是:

    if ($this->id_member != null) {
                $sql = "UPDATE `tbl_member`
                        SET `status` = 1
                        WHERE `id_member` = {$this->id_member};";
                // echo $sql;
                $result = $this->database->query($sql);
}

错误讯息:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近''第3行{"状态":"成功"}

我尝试在列和表名上使用反斜杠,但错误仍然出现。我在这里检查了与此相关的stackoverflow问题,但没有一个能解决我的问题。 感谢

echo $ sql的结果是

UPDATE `tbl_member`
                    SET `status` = 1
                    WHERE `id_member` = 96;{"status":"success"}

这方面的ajax是

function activateSubscriber() {
    $.ajax({
        url: "../../system/components/subscribers/controller/activate_subscriber.php",
        type: "POST",
        dataType: "json",
        data: $('#activate_subscriber_form').serialize(),
        success: function (data) {
            if (data.status == "success") {
                $('#activate_subscriber_modal').modal('hide');
                getMemberList();
                $.notify({
                    icon: 'glyphicon glyphicon-warning-sign',
                    title: 'Success',
                    message: 'Subscriber was successfully activated.',
                }, {
                    type: 'success'
                });
            } else {
                $.notify({
                    icon: 'glyphicon glyphicon-warning-sign',
                    title: 'Failed',
                    message: 'Subscriber was NOT successfully activated.',
                }, {
                    type: 'danger'
                });
            }
        }
    });
}

控制器是:

<?php

include('../../../config.php');
require('../../../classes/mysql.php');
include('../model/member.php');

$subscriber = new member($_POST['id_member'], $db_data);
$subscriber_result = $subscriber->activateSubscriber();
echo(json_encode($subscriber_result))

模型是

public function activateSubscriber() {
        try {
            if ($this->id_member != null) {
                $sql = "UPDATE tbl_member SET status = 1 WHERE id_member = {$this->id_member}";
                // echo $sql;
                $result = $this->database->query($sql);

                if ($result) {
                    return array('status' => 'success');
                } else {
                    return array('status' => 'failed');
                }
            } else {
                return array(
                    'error' => "id_member can't be null."
                );
            }
        } catch (Exception $ex) {
            echo $ex->getMessage() . '  Errror!!';
        }
    }

4 个答案:

答案 0 :(得分:1)

如果status是varchar,则将其值设为单引号' 试试这个:

$sql = "UPDATE `tbl_member`
                    SET `status` = '1'
                    WHERE `id_member` = {$this->id_member}";

答案 1 :(得分:1)

  if ($this->id_member != null) {
            $sql = "UPDATE `tbl_member`
                    SET `status` = 1
                    WHERE `id_member` = ".$this->id_member;
            // echo $sql;
            $result = $this->database->query($sql); }

答案 2 :(得分:1)

尝试这个(添加async:false)到你的ajax调用并检查。

$.ajax({
        url: "../../system/components/subscribers/controller/activate_subscriber.php",
        type: "POST",
        async:false,
        ...
});

答案 3 :(得分:0)

我终于解决了这个问题。这很有趣。在我的构造函数中,我有一个检索当前成员详细信息的函数。我的数据库中已经存在一个不再存在的字段。它试图在我的while循环中检索不存在的字段的值时遇到困难。

感谢大家的帮助和帮助。我很感激!!