A
A
Anton Shtinov2015-07-16 18:57:42
PHP
Anton Shtinov, 2015-07-16 18:57:42

Why is the insertion into the database duplicated?

$dbh = new PDO('sqlite:'.$filename);
$dbh->exec("INSERT INTO test VALUES ('test1')");
$dbh->exec("INSERT INTO test VALUES ('test2')");


$sth = $dbh->query('SELECT * FROM test');
$sth->setFetchMode(PDO::FETCH_ASSOC);
while($row = $sth->fetch()) print_r($row);

The insert code should insert two rows into the table, the subsequent selection confirms this, as it were. However, after viewing the contents of the table in the database editor, it turns out that there are 4 rows.
And if, after inserting rows, call:
$dbh = new PDO('sqlite:'.$filename);
$sth = $dbh->query('SELECT * FROM test');
while($row = $sth->fetch()) print_r($row);

then new lines are added with each call. I can't figure out what is the root of the problem.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Shtinov, 2015-07-17
@delimer

In general, I tried to do this with a new base - the insert is duplicated. Most of all, it is not clear why, if you first make an insert, and then only do a select, then the data is still inserted. I tried to work with the database through PDO and directly with SQLite3 - the result is the same. I'm inclined to believe that the glitch is somewhere on the server side.
As a result, the glitch was found on the server side. There were some strange rules in htaccess that, along with any file, always launched index.php.

D
Dmitry Kovalsky, 2015-07-16
@dmitryKovalskiy

Not familiar with sqlite, but is the field you're inserting a value into a key field?

A
Andrew, 2015-07-16
@Ashlst

Give the structure of the test table, please.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question