php - 查询错误: where is the error?

标签 php mysql

我收到以下错误:

[07-Mar-2011 04:52:31] exception 'Exception' in /home1/mexautos/public_html/kiubbo/data/model.php:89
Stack trace:
#0 /home1/mexautos/public_html/kiubbo/data/article.php(276): Model::execSQl2('update articles...')
#1 /home1/mexautos/public_html/kiubbo/data/article.php(111): Article->save()
#2 /home1/mexautos/public_html/kiubbo/pages/frontpage.php(21): Article->calculateRanking()
#3 /home1/mexautos/public_html/kiubbo/pages/frontpage.php(27): FrontPage->updateRanking()
#4 /home1/mexautos/public_html/kiubbo/index.php(15): FrontPage->showTopArticles('')
#5 {main}

这是行:$lastid = Parent::execSql2($query);

这是代码,如果有人可以帮助我找到错误所在:

function save() {

/*
        Here we do either a create or
        update operation depending
        on the value of the id field.
        Zero means create, non-zero
        update
*/

    if(!get_magic_quotes_gpc())
    {
        $this->title = addslashes($this->title);
        $this->description = addslashes($this->description);
    }

    try
    {
        $db = parent::getConnection();
        if($this->id == 0 )
        {
            $query = 'insert into articles (modified, username, url, title, description, points )';
            $query .= " values ('$this->getModified()', '$this->username', '$this->url', '$this->title', '$this->description', '$this->points' )";

            }
        else if($this->id != 0)
        {
                $query = "update articles set modified = CURRENT_TIMESTAMP, username = '$this->username', url = '$this->url', title = '$this->title', description = '$this->description', points = '$this->points', ranking = '$this->ranking' where id = '$this->id' ";
            }

        $lastid = parent::execSql2($query);

        if($this->id == 0 )
            $this->id = $lastid;

    }
    catch(Exception $e){
        error_log($e);
    }
}

最佳答案

您必须将变量括在{}标签中;像这样

$query .= " values ('{$this->getModified()}', '{$this->username}', '{$this->url}', '{$this->title}', '{$this->description}', '{$this->points}' )";

大括号让 PHP 知道不要将文本视为文字。请注意,这仅适用于双引号字符串。 (编辑了代码;我忘记了每个值周围的单引号)

回显字符串可能会有所帮助,以便您可以检查将要执行的内容。

关于php - 查询错误: where is the error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5219884/

相关文章:

php - 将值传递到外部 js 文件

php - 我应该使用 <object> 还是 &lt;iframe&gt; 来加载视频?

MySQL INSERT 和基于匹配列的 SELECT

php - 使用jquery上传文件而不刷新页面

php - 我应该在哪里注册我的 DBAL 类型?

javascript - 使用PHP+JSON+AJAX清除以前从数据库返回的HTML数据

php - 多选 SQL 中某些值返回 NULL

mysql - 损坏的 mysqld 包阻止在 ubuntu 中进行任何其他安装

mysql_query 插入到表中

MySQL 选择带有可变空格的字符串中最后一个空格左侧的所有内容