PDO Inserts data twice in a single query

I have a terrible problem with pdo statements. My class generates an Object-based SQL Query, then redirects the query and params to the Bd Class and executes, but the data is inserted twice into the 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 request

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

Generated Array of Parameters

 Array ( [0] => 1 ) 

db function call

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

Bd Class (only the functions used for this task)

  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.

+6
source share
3 answers

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

+1
source

Why are you trying to insert a NULL column into an id_object column if NULL not allowed for column constraints?

Instead, should you leave id_object from the INSERT and let MySQL add a new auto-increment value here?

+3
source

Try adding some entries to the page that calls _execute (), and include a timestamp. See for any reason a page is requested more than once.

If so, you should be able to track if this is related to something in .htaccess, etc.

If this does nothing for you, try commenting

 $this->result = $pdoStatement; return $pdoStatement; 

This way you can check if this is related to your call to $ pdoStatement.

0
source

Source: https://habr.com/ru/post/988788/


All Articles