V
V
Vyacheslav Lebedev2014-08-04 20:51:32
PHP
Vyacheslav Lebedev, 2014-08-04 20:51:32

Why are 2 new records added to the database table at once, and not one?

When the page is refreshed, 2 records are added to the table. I can't find in the code why 2 and not one.
Help, I’ve been poking here and there for about 30 minutes... And query() is executed 1 time, but why are there 2 entries :( I
use it to connect to the PDO database .
I post codes:
index.php

$sth = $dataBase->insert('tasks', array('task_title' => 'Третья запись', 'task_desc' => 'Ура, появилась третья запись!'));
echo '<br>';
var_dump($sth);

Base file:
public function insert($tableName, $row = array()) {
    if (!count($row)) return false;
    $tableName = $this->getTableName($tableName); // Вернет имя таблицы с учетом префикса
    $fields = '(';
    $values = 'VALUES (';
    $params = array();
    foreach ($row as $key => $value) {
      $fields .= "`$key`,";
      $values .= "?,";
      $params []= $value;
    }
    // Удаление последнего символа ','
    $fields = substr($fields, 0, -1).')';
    $values = substr($values, 0, -1).')';
    $query = "INSERT INTO `$tableName` $fields $values";

    echo $query.'<br>';
    var_dump($params);

    return $this->query($query, $params);
  }

        private function query($query, $params) {
    echo '<br>YO!';
    $STH = $this->_DBH->prepare($query);
    if (!$STH) return false;
    if (isset($params)) $STH->execute($params);
    else $STH->execute();
    // Если НЕ производилась вставка, вернет true, иначе вернет id вставленной записи
    if ($this->_DBH->lastInsertId() === 0) return true;
    else return $this->_DBH->lastInsertId();
  }

ps: I don't think I forgot to add anything.
UPD: The answer was in the .htaccess settings, namely in these lines:
RewriteCond %{REQUST_FILENAME} !-f
RewriteCond %{REQUST_FILENAME} !-d
RewriteRule .* index.php [L]

Navigating to index.php generated a redirect to index.php .
Which was absolutely unpredictable behavior for me :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav Lebedev, 2014-08-05
@slavikse

Zero help :) Cool.
For the super dumb, index.php:

<?php

require_once 'sys/config/config.class.php';
require_once 'sys/core/abstractdatabase.class.php';

$dataBase = new AbstractDataBase(Config::$dbHost, Config::$dbName, Config::$dbUser, Config::$dbPass, Config::$dbPrefix);

$sth = $dataBase->insert('tasks', array('task_title' => 'Четвертая запись!', 'task_desc' => 'Именно четвертая, не пятая и не шестая!'));

echo '<br><pre>';
var_dump($sth);
echo '</pre>';

execute() - called 1 time, be careful before writing about this...

E
Evgeny Elchev, 2014-08-05
@rsi

30 is not a deadline to create questions, open xdebug for yourself, it will be easier to look for such jambs. In general, do not be lazy to use curly braces, then you will also notice the obvious things.

if (!$STH) return false;
       if (isset($params)) $STH->execute($params);
else $STH->execute();

Call execute twice

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question