There is a registration page:
<?php require_once("config.php"); if (!empty($_SESSION['user_id'])) { header("location /index.php"); } // обработка ошибок $errors = []; if (!empty($_POST)) { $validator = new Validator(new DB()); // Передаем валидатору объект базы данных foreach ($_POST as $k => $v) { $validator->checkEmpty($k, $v); } $validator->checkMaxLength('user_name', $_POST['user_name'], 'users', 'username'); $validator->checkMaxLength('first_name', $_POST['first_name'], 'users', 'first_name'); $validator->checkMaxLength('last_name', $_POST['last_name'], 'users', 'last_name'); $validator->checkMinLength('password', $_POST['password'], 6); $validator->checkMatch('password', $_POST['password'], 'confirm_password', $_POST['confirm_password']); $errors = $validator->errors; // Добавлениею юзера в базу if (empty($errors)) { $user = new User(); //Присваиваем свойствам объекта user значения из POST запроса $user->userName= $_POST['user_name']; $user->email= $_POST['email']; $user->password= sha1($_POST['password'].SALT); $user->firstName= $_POST['first_name']; $user->lastName= $_POST['last_name']; $user->save(); // Сохраняем юзера в базу header("location: /login.php?registration=1"); } } ?> <!DOCTYPE html> <html> <head> <title>Страница регистрации</title> <meta charset="UTF-8"> </head> <form method="POST"> <div style="color:red;"> <?php foreach ($errors as $error) :?> <p><?php echo $error;?></p> <?php endforeach; ?> </div> <div> <label>username</label> <div> <input type="text" name="user_name" required="" value="<?php echo (!empty($_POST['user_name']) ? $_POST['user_name'] : '');?>"/> </div> </div> <div> <label>Email</label> <div> <input type="email" name="email" required="" value="<?php echo(!empty($_POST['email']) ? $_POST['email'] : '');?>"/> </div> </div> <div> <label>First Name</label> <div> <input type="text" name="first_name" required="" value="<?php echo(!empty($_POST['first_name']) ? $_POST['first_name'] : '');?>"/> </div> </div> <div> <label>Last Name</label> <div> <input type="text" name="last_name" required="" value="<?php echo(!empty($_POST['last_name']) ? $_POST['last_name'] : '');?>"/> </div> </div> <div> <label>password</label> <div> <input type="password" name="password" required="" value=""/> </div> </div> <div> <label>Повторите пароль</label> <div> <input type="password" name="confirm_password" required="" value=""/> </div> </div> <div> <hr/> <input type="submit" name="submit" value="Зарегистрироваться"> </div> </form> </div> </body> </html> Validator Class:
<?php class Validator { private $_db; public $errors = []; public function __construct($db) { $this->_db = $db; } public function checkEmpty($name, $value) { $name = ucfirst(str_replace("_", " ", $name)); if (empty ($value)) { return $this->errors[] = "Введите" . $name; } else { return 0; } } //Соответствие паролей public function checkMatch($name1, $value1, $name2, $value2) { $name1 = ucfirst(str_replace("_", " ", $name1)); $name2 = ucfirst(str_replace("_", " ", $name2)); if ($value1 !== $value2) { return $this->errors[] = "Your " . $name2. "is not match " . $name1; } else { return 0; } } //Возвращает максимальную длину поля public function checkMaxLength($name, $value, $table, $column) { $name = ucfirst(str_replace("_", " ", $name)); $maxLength = $this->_db->getMaxLength($table, $column); if (strlen($value) > $maxLength) { return $this->errors[] = $name . "is too long . Max length is " . $maxLength."characters"; } else { return 0; } } // Возвращает минимально допустимое значение поля public function checkMinLength($name, $value, $minLength) { $name = ucfirst(str_replace("_"," ", $name )); if (strlen($value) < $minLength) { return $this->errors[] = $name. "Должно содержать не менее" . $minLength . "символов"; } else { return 0; } } } введите сюда код and connect to the database
<? class DB { protected $conn = null; //Конект к БД private $_host = HOST; private $_dbname = DBNAME; private $_user = USER; private $_password = PASSWORD; private $_error; public function __construct() { $dsn = "mysql:host=" . $this->_host. ";dbname=" .$this->_dbname. ";charset=utf8"; try { $this->conn = new PDO($dsn, $this->_user, $this->_password); } catch (PDOException $e) { $this->conn = null; $this->_error = $e->getMessage(); } } public function getError() { return $this->_error; } // Получить максимальную длину любого поля public function getMaxLength($table, $column) { $stmt = $this->conn->prepare('select COLUMN_NAME, CHARACTER_MAXIMUM_LENGTH from information_scema.columns where table_schema = DATABASE() AND table_name = :table AND COLUMN_NAME = :column'); $stmt->execute(array('table' => $table, 'column' => $column)); $column = $stmt->fetch(PDO::FETCH_LAZY); return $column['CHARACTER_MAXIMUM_LENGTH']; } } When submitting the registration form, the validator issues this: 
Can someone tell me the reason for the wrong form work? Why does the validator remap $ name?
UPD1:
If you hard set $ maxLength = 100; Then the validator works correctly, but I wanted the information about max. and the minimum length was taken exactly from the database (the request itself is lower)
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar (100) NOT NULL, `email` varchar(150) NOT NULL, `password` varchar(100) NOT NULL, `first_name` varchar(40) NOT NULL, `last_name` varchar(40) NOT NULL, `created_at` timestamp DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `comment` text NOT NULL, `created_at` timestamp DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;