不插入数据库的语句
statement not inserting into database
我正在使用此 class 代码将数据插入数据库:
<?php
Class User
{
private $error = "";
public function signup($post)
{
$data = array();
$data['name'] = trim($_POST['name']);
$data['email'] = trim($_POST['email']);
$data['pass'] = trim($_POST['pass']);
$pass2 = trim($_POST['pass2']);
if(empty($data['email']) || !preg_match("/^[0-9a-zA-Z_-]+@[0-9a-zA-Z_-]+.[0-9a-zA-Z_-]+$/", $data['email']))
{
$this->error .= "Please enter a valid Email <br>";
}
if(empty($data['name']) || !preg_match("/^[a-zA-Z]+$/", $data['name']))
{
$this->error .= "Please enter a valid Name <br>";
}
if($data['pass'] !== $pass2)
{
$this->error .= "Password dont match <br>";
}
if(strlen($data['pass']) < 4)
{
$this->error .= "Password must be at least 4 character long <br>";
}
if($this->error == "")
{
$data['rank'] = "customer";
$data['url_address'] = $this->get_random_string_max(60);
$data['date'] = date("Y-m-d H:i:s");
$query = "INSERT INTO users (url_address, name, email, pass, date, rank) VALUES (:url_address, :name, :email, :pass, :date, :rank)";
$db = Database::getInstance();
$result = $db->write($query, $data);
if ($result) {
header("Location:" . ROOT . "login");
die();
}
}
}
private function get_random_string_max($length)
{
$array = array(0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$text = "";
$length = rand(4, $length);
for ($i=0; $i>$length;$i++) {
$random = rand(0, 61);
$text .= $array[$random];
}
return $text;
}
}
这是数据库 Class:
<?php
Class Database
{
public static $con;
public function __construct()
{
$dsn = 'mysql:hostname=localhost;dbname='. DB_NAME;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
return $db = new PDO($dsn, DB_USER, DB_PASS, $options);
} catch (PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance()
{
if(self::$con) {
return self::$con;
}
return $instance = new self();
}
public function read($query, $data = array())
{
$stmt = self::$con->prepare($query);
$result = $stmt->excute($data);
if ($result) {
$data = $stmt->fetchAll(PDO::FETCH_OBJ);
if(is_array($data)) {
return $data;
}
}
return false;
}
public function write($query, $data = array())
{
$stmt = self::$con->prepare($query);
$result = $stmt->excute($data);
if ($result) {
return true;
}
return false;
}
}
我正在使用 MVC,这是注册控制器页面和数据库 class。
pass2 是在表单中重新输入的密码。
但是数据没有插入到数据库中,也没有重定向到登录页面。
和
PDO::ERRMODE_EXCEPTION
没有显示任何错误。
有什么问题?
在数据库中 Class 您在编写函数 excute() 时出错:
$result = $stmt->excute($data);
应该是execute()
$result = $stmt->execute($data);
我正在使用此 class 代码将数据插入数据库:
<?php
Class User
{
private $error = "";
public function signup($post)
{
$data = array();
$data['name'] = trim($_POST['name']);
$data['email'] = trim($_POST['email']);
$data['pass'] = trim($_POST['pass']);
$pass2 = trim($_POST['pass2']);
if(empty($data['email']) || !preg_match("/^[0-9a-zA-Z_-]+@[0-9a-zA-Z_-]+.[0-9a-zA-Z_-]+$/", $data['email']))
{
$this->error .= "Please enter a valid Email <br>";
}
if(empty($data['name']) || !preg_match("/^[a-zA-Z]+$/", $data['name']))
{
$this->error .= "Please enter a valid Name <br>";
}
if($data['pass'] !== $pass2)
{
$this->error .= "Password dont match <br>";
}
if(strlen($data['pass']) < 4)
{
$this->error .= "Password must be at least 4 character long <br>";
}
if($this->error == "")
{
$data['rank'] = "customer";
$data['url_address'] = $this->get_random_string_max(60);
$data['date'] = date("Y-m-d H:i:s");
$query = "INSERT INTO users (url_address, name, email, pass, date, rank) VALUES (:url_address, :name, :email, :pass, :date, :rank)";
$db = Database::getInstance();
$result = $db->write($query, $data);
if ($result) {
header("Location:" . ROOT . "login");
die();
}
}
}
private function get_random_string_max($length)
{
$array = array(0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$text = "";
$length = rand(4, $length);
for ($i=0; $i>$length;$i++) {
$random = rand(0, 61);
$text .= $array[$random];
}
return $text;
}
}
这是数据库 Class:
<?php
Class Database
{
public static $con;
public function __construct()
{
$dsn = 'mysql:hostname=localhost;dbname='. DB_NAME;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
return $db = new PDO($dsn, DB_USER, DB_PASS, $options);
} catch (PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance()
{
if(self::$con) {
return self::$con;
}
return $instance = new self();
}
public function read($query, $data = array())
{
$stmt = self::$con->prepare($query);
$result = $stmt->excute($data);
if ($result) {
$data = $stmt->fetchAll(PDO::FETCH_OBJ);
if(is_array($data)) {
return $data;
}
}
return false;
}
public function write($query, $data = array())
{
$stmt = self::$con->prepare($query);
$result = $stmt->excute($data);
if ($result) {
return true;
}
return false;
}
}
我正在使用 MVC,这是注册控制器页面和数据库 class。
pass2 是在表单中重新输入的密码。
但是数据没有插入到数据库中,也没有重定向到登录页面。
和 PDO::ERRMODE_EXCEPTION
没有显示任何错误。
有什么问题?
在数据库中 Class 您在编写函数 excute() 时出错:
$result = $stmt->excute($data);
应该是execute()
$result = $stmt->execute($data);