微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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." );
    }
}

但是,每次出现此错误“2012-05-11 14:40:50:sqlSTATE [42000]:语法错误或访问冲突:1064您的sql语法有错误;请查看与您的MysqL服务器对应的手册在’desc,datecreated,isactive’附近使用正确语法的版本VALUES(‘d5989c7e-9b98-11e1-88cd-0026b936528c’,第1行’1′

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

解决方法:

在反引号(`)中封装你的字段名称,desc是一个保留字.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐