douhezi2285 2015-06-09 14:33
浏览 73
已采纳

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

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.

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)

    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条回答 默认 最新

  • duanjuan3931 2015-06-14 11:21
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置