PDO在单个查询中插入数据两次

时间:2022-11-20 16:26:10

I have a terrible problem with pdo statements. My class generate a SQL Query based on the Object, then forward the query and params to Bd Class and execute, but data is inserted twice to database.

我对pdo语句有一个严重的问题。我的类基于对象生成一个SQL查询,然后将查询和解析转发到Bd类并执行,但是数据被插入到数据库中两次。

Table in database

表在数据库

CREATE TABLE IF NOT EXISTS `es_simple_object` (
    `id_object` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `active` tinyint(1) NOT NULL,

    PRIMARY KEY (`id_object`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Generated query

生成的查询

INSERT INTO es_simple_object (es_simple_object.id_object, es_simple_object.active) VALUES (NULL, ?)

Generated params array

生成的参数数组

Array
(
    [0] => 1
)

call Db function

调用数据库函数

static::$db = Db::getInstance();    
static::$db->_execute($sql, $params);

Bd Class (just functions used to this job)

Bd类(只是用于此作业的函数)

    public static function getInstance()
    {
        if (!isset(self::$instance))
            self::$instance = new Db();

        return self::$instance;
    }

    private function __construct()
    {
        $connection = 'mysql:host='.$this->server.'; port='.$this->port.'; dbname='._DB_NAME_.'; charset='._DB_CHARSET_;

        try 
        {
            $this->link = new PDO($connection, _DB_USER_, _DB_PASSWD_);
            $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } 
        catch (PDOException $ex) 
        {   
            Tools::_catchException($ex);

            exit;
        }

        return $this->link;
    }

    public function _execute($sql, $params = array())
    {
        try
        {           
            $pdoStatement = $this->link->prepare($sql);
            $pdoStatement->execute($params == null ? array(null) : $params);

            $this->rows_affected = $pdoStatement->rowCount();
            $this->rows_returned = $pdoStatement->columnCount();
            $this->last_id = $this->link->lastInsertId();
            $this->result = $pdoStatement;

            return $pdoStatement;
        }
        catch (PDOException $ex) 
        {
            Tools::_catchException($ex, array($sql, $params));

            return false;
        }
    }

I have no more ideas how to solve this problem

我对如何解决这个问题束手无策

3 个解决方案

#1


3  

Why are you attempting to insert a NULL to the id_object column, when NULL is not allowed per the column constraints?

为什么要向id_object列插入一个NULL,而每个列约束都不允许使用NULL ?

Instead, shouldn't you perhaps leave id_object out of the INSERT statement, and let MySQL add a new autoincremented value there?

相反,难道不应该将id_object放在INSERT语句之外,让MySQL在其中添加一个新的自动递增值吗?

#2


1  

Thanks to @James Taylor for the suggestion of .htaccess file. The problem was hiding in RewriteRule and parameter QSA.

感谢@James Taylor对.htaccess文件的建议。问题隐藏在RewriteRule和参数QSA中。

#3


0  

Try adding some logging on the page that calls _execute() and include a timestamp. See if for some reason the page is being requested more than once.

尝试在调用_execute()的页面上添加一些日志记录,并包含一个时间戳。看看是否出于某种原因,页面被请求了多次。

If that is the case you should be able to track down whether it is due to something in .htaccess etc.

如果是这样,您应该能够跟踪它是否是由于。htaccess等中的某些东西。

If that does nothing for you, try commenting out

如果这对你没有任何帮助,试着注释掉

$this->result = $pdoStatement;

        return $pdoStatement;

This way you can test to see if it has something to do with your calling $pdoStatement again.

通过这种方式,您可以测试它是否与您再次调用$pdoStatement有关。

#1


3  

Why are you attempting to insert a NULL to the id_object column, when NULL is not allowed per the column constraints?

为什么要向id_object列插入一个NULL,而每个列约束都不允许使用NULL ?

Instead, shouldn't you perhaps leave id_object out of the INSERT statement, and let MySQL add a new autoincremented value there?

相反,难道不应该将id_object放在INSERT语句之外,让MySQL在其中添加一个新的自动递增值吗?

#2


1  

Thanks to @James Taylor for the suggestion of .htaccess file. The problem was hiding in RewriteRule and parameter QSA.

感谢@James Taylor对.htaccess文件的建议。问题隐藏在RewriteRule和参数QSA中。

#3


0  

Try adding some logging on the page that calls _execute() and include a timestamp. See if for some reason the page is being requested more than once.

尝试在调用_execute()的页面上添加一些日志记录,并包含一个时间戳。看看是否出于某种原因,页面被请求了多次。

If that is the case you should be able to track down whether it is due to something in .htaccess etc.

如果是这样,您应该能够跟踪它是否是由于。htaccess等中的某些东西。

If that does nothing for you, try commenting out

如果这对你没有任何帮助,试着注释掉

$this->result = $pdoStatement;

        return $pdoStatement;

This way you can test to see if it has something to do with your calling $pdoStatement again.

通过这种方式,您可以测试它是否与您再次调用$pdoStatement有关。