laminas/laminas-db

Insert API insert record 2 times

Closed this issue · 3 comments

Bug Report

Q A
Version(s) 2.11.2

Summary

I am playing around with laminas-db and I notice something ode when inserting data, the row is added twice.

Current behavior

It duplicate the record in the table on insert.

How to reproduce

My table definition :

CREATE TABLE `ps_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `value` text,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

The code :

$adapter = new Adapter([
    'driver' => 'Pdo_Mysql',
    'database' => 'ps1762',
    'username' => 'root',
    'password' => 'root',
    'hostname' => 'localhost',
    'port' => 3306,
    'charset' => 'utf8mb4'
]);

$sql = new Sql($adapter, 'ps_test');

$insert = $sql->insert();
$insert->values([
    'name' => 'hello',
    'value' => 'world'
]);

$statement = $sql->prepareStatementForSqlObject($insert);
$result = $statement->execute();

if ($result instanceof ResultInterface && $result->getAffectedRows()) {
    echo 'Success';
} else {
    echo 'Failure';
}

When I go check in the table after execution, there are 2 rows added with the same data instead of just 1.

When I check the query with var_dump($sql->buildSqlString($insert)); it is all good :
string 'INSERT INTO ps_test (name, value) VALUES ('hello', 'world')' (length=65)

The Result I got :

object(Laminas\Db\Adapter\Driver\Pdo\Result)[19]
  protected 'statementMode' => string 'forward' (length=7)
  protected 'fetchMode' => int 2
  protected 'resource' => 
    object(PDOStatement)[24]
      public 'queryString' => string 'INSERT INTO `ps_test` (`name`, `value`) VALUES (:c_0, :c_1)' (length=59)
  protected 'options' => null
  protected 'currentComplete' => boolean false
  protected 'currentData' => null
  protected 'position' => int -1
  protected 'generatedValue' => string '84' (length=2)
  protected 'rowCount' => null

Expected behavior

Only 1 record inserted, no duplicate.

@debuss

It duplicate the record in the table on insert.

Unfortunately I can't reproduce your problem.

Please check that your script is only executed once and if the usage of plain PDO produces also two entries in the database.

And please try the following script:

require_once __DIR__ . '/vendor/autoload.php';

$adapter = new Laminas\Db\Adapter\Adapter([
    'driver' => 'Pdo',
    'dsn'    => 'sqlite::memory:',
]);

$sql = new Laminas\Db\Sql\Sql($adapter);

// Create table
$table = new Laminas\Db\Sql\Ddl\CreateTable('test');
$table->addColumn(new Laminas\Db\Sql\Ddl\Column\Varchar('name', 255));
$table->addColumn(new Laminas\Db\Sql\Ddl\Column\Varchar('value', 255));
$adapter->query(
    $sql->buildSqlString($table),
    $adapter::QUERY_MODE_EXECUTE
);

// Insert data
$insert = $sql->insert('test');
$insert->values([
    'name'  => 'hello',
    'value' => 'world',
]);

$statement = $sql->prepareStatementForSqlObject($insert);
$result    = $statement->execute();

var_dump($result->getAffectedRows()); // 1

// Fetch data
$select = $sql->select('test');
$statement = $sql->prepareStatementForSqlObject($select);
$result    = $statement->execute();

var_dump($result->count()); // 1
var_dump($result->current()); // ['name' => 'hello', 'value' => 'world']

If the script produces the same result for you, please move your question to the forum to get support for your problem.

@debuss How do you run it? Any chance you trigger it via the browser? Could it be that second request browsers send for favicon triggered it again?

I tried on an other computer and I can't reproduce, I suppose the issue comes from the browser indeed.
Let's close this, sorry for the inconvenience.