PHP PDO 语法错误或访问冲突:插入时为 1064

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10556941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:27:18  来源:igfitidea点击:

PHP PDO Syntax error or access violation: 1064 on insert

phpmysqlpdo

提问by Naterade

I have a table name boards, and I'm trying to insert data using the current code

我有一个表名板,我正在尝试使用当前代码插入数据

function createBoard( $name, $desc  ) {
    try {
        $sth = $this->getLink()->prepare( "INSERT INTO boards(id,memberid,name,desc,datecreated,isactive) 
            VALUES(?,?,?,?,?,?)" );

        $uuid = $this->uuid();
        $memberid = 1;
        $date = Utils::getDate();
        $isactive = 1;

        $sth->bindParam( 1, $uuid );
        $sth->bindParam( 2, $memberid );
        $sth->bindParam( 3, $name );
        $sth->bindParam( 4, $desc );
        $sth->bindParam( 5, $date );
        $sth->bindParam( 6, $isactive );
        return $sth->execute();
    } catch( PDOException $e ) { 
        /*
         * save the error to the error log defined as @ERROR_LOG
         */ 
        file_put_contents( ERROR_LOG, ( "\n" . Utils::getDate() . " : " . $e->getMessage() ), FILE_APPEND);  
        die( "FATAL ERROR...Please check the error log." );
    }
}

However, every time I get this error "2012-05-11 14:40:50 : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc,datecreated,isactive) VALUES('d5989c7e-9b98-11e1-88cd-0026b936528c','1' at line 1"

但是,每次我收到此错误“ 2012-05-11 14:40:50 : SQLSTATE[42000]: Syntax error or access conflict: 1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器相对应的手册'desc,datecreated,isactive) VALUES('d5989c7e-9b98-11e1-88cd-0026b936528c','1' at line 1 附近使用的正确语法的版本"

I have tried the same function using bindValue()and just putting the array with direct values into the $sth->execute()function but I always receive the same error. Maybe an outside eye can spot something I'm missing or doing wrong?

我已经尝试使用相同的函数bindValue(),只是将具有直接值的数组放入$sth->execute()函数中,但我总是收到相同的错误。也许外面的眼睛可以发现我遗漏或做错的事情?

回答by ccKep

Encapsulate your field names in backticks (`), descis a reserved word.

用反引号 ( `)封装您的字段名称,这desc是一个保留字。

$sth = $this->getLink()->prepare( "INSERT INTO `boards` (`id`,`memberid`,`name`,`desc`,`datecreated`,`isactive`) 
            VALUES(?,?,?,?,?,?)" );