Answer the question
In order to leave comments, you need to log in
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);
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();
}
RewriteCond %{REQUST_FILENAME} !-f
RewriteCond %{REQUST_FILENAME} !-d
RewriteRule .* index.php [L]
Answer the question
In order to leave comments, you need to log in
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>';
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();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question