function create($object) //создает строку (из объекта) (любые таблицы)
{
$table = $object->tablename;
unset($object->tablename);
$keys = [];
$values = [];
$columns = [];
foreach ($object as $key => $value) {
$keys[] = $key;
$columns[$key] = ':' . $key;
$values[$key] = $value;
}
$stmt = $this->prepare('INSERT INTO ' . $table . ' (' . implode(',', $keys) . ') VALUES (' . implode(',', $columns) . ')');
foreach ($keys as $key) {
$stmt->bindValue($columns[$key], $values[$key], is_integer($values[$key]) ? PDO::PARAM_INT : PDO::PARAM_STR);
}
$stmt->execute();
}
function insert($o)
{
$t = $o->tablename;
unset($o->tablename);
$keys = [];
$values = [];
foreach ($o as $k => $v) {
$keys[] = $k;
if (empty($v)): $values[] = 'null';
else: $values[] = is_string($v) ? "'{$v}'" : $v; endif;
}
$keys = implode(',', $keys);
$values = implode(',', $values);
$this->query('INSERT INTO ' . $t . ' (' . $keys . ') VALUES (' . $values . ')');
}
$text = 'текст: '.$var;
function insert($o){
$t = $o->tablename;
unset($o->tablename);
$keys = array();
$values = array();
foreach ($o as $k => $v){
$keys[] = $k;
$values[] = $v;
}
$this->query('INSERT INTO '.$t.' ('.implode($keys).') VALUES('.implode($values).')');
}
<?php
require 'config/config.php';
$errors = array();
if (!empty($_COOKIE['sid']) and empty($_SESSION['logged_user'])) {
$user_id = $conn->findColumn('SELECT user_id FROM access WHERE hash = ?', array($_COOKIE['sid']));
if (!empty($user_id)) {
$user = $conn->findOne('users', 'id = ?', array($user_id));
if (!empty($user)) {
setcookie('sid', $_COOKIE['sid'], time() + 60 * 60 * 24 * 365);
$_SESSION['logged_user'] = array(
'id' => $user->id,
'login' => $user->login,
'type' => $user->type,
'hash' => $_COOKIE['sid']
);
} else {
setcookie('sid', null, -1);
}
}
}
if (empty($_SESSION['logged_user'])) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$user = $conn->findOne('users', 'login = ?', array($_POST['login']));
if (!empty($user)) {
if ($user->password == $_POST['password']) {
$hash = bin2hex(random_bytes(20));
$conn->cudOne('INSERT INTO access (user_id,hash) VALUES (?, ?)', array($user->id, $hash));
setcookie('sid', $hash, time() + 60 * 60 * 24 * 365);
$_SESSION['logged_user'] = array(
'id' => $user->id,
'login' => $user->login,
'type' => $user->type,
'hash' => $hash
);
}
} else {
$errors[] = "Пользователь не найден";
}
}
}
session_name($_COOKIE['sid'];
session_start();