<?php
include "session.php";
include "Database.php";
class User{
private $db;
public function __construct(){
$this->db = new Database();
}
public function userRegistration($data){
$name = $data['name'];
$usnm = $data['usnm'];
$email = $data['email'];
$pass = md5($data['pass']);
$chk_email = $this->emailcheck($email);
if($name == "" OR $usnm == "" OR $email == "" OR $pass == ""){
$msg = "<div class='alert alert-denger'><strong>Error !</strong>Field must not be empty</div>";
return $msg;
}
if(strlen($usnm) < 3){
$msg = "<div class='alert alert-denger'>username too short!</div>";
return $msg;
}
elseif(preg_match('/[^a-z0-9_-]+/i' ,$usnm)){
$msg = "<div class='alert alert-denger'>Username must in alphanumeric!</div>";
return $msg;
}
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$msg = "<div class='alert alert-denger'>Invalid Email Id!</div>";
return $msg;
}
if($chk_email == true){
$msg = "<div class='alert alert-denger'>Email Id Already Exist!</div>";
return $msg;
}
$sql = "insert into register (name,user_name,email_id,password) values (:name, :username, :email, :password)";
$query = $this->db->pdo->prepare($sql);
$query->bindValue(':name',$name);
$query->bindValue(':user_name',$usnm);
$query->bindValue(':email_id',$email);
$query->bindValue(':password',$pass);
$result = $query->execute();
if($result){
$msg = "<div class='alert alert-success'><strong>Success</strong>You have been Registered</div>";
return $msg;
}else{
$msg = "<div class='alert alert-denger'><strong>Error !</strong>Sorry, there has been problem inserting your details!</div>";
return $msg;
}
}
public function emailcheck($email){
$sql = "select email_id from register email_id = :email";
$query = $this->db->pdo->prepare($sql);
$query->bindparam(':email_id', $email);
$query->execute();
if($query->rowCount() > 0){
return true;
}else{
return false;
}
}
}
?>
我收到了这个错误
致命错误:未捕获的异常&#39; PDOException&#39;与消息 &#39; SQLSTATE [HY093]:参数号无效:参数未定义&#39; 在C:\ xampp \ htdocs \ test \ lib \ user.php:71堆栈跟踪:#0 C:\ xampp \ htdocs \ test \ lib \ user.php(71):PDOStatement-&gt; execute()#1 C:\ XAMPP \ htdocs中\测试\ lib中\ user.php的(19): 用户 - &gt;电子邮件检查(&#39; jpjadeja24 @ gmai ...&#39;)#2 C:\ xampp \ htdocs \ test \ register.php(7):User-&gt; userRegistration(Array)#3 在第71行的C:\ xampp \ htdocs \ test \ lib \ user.php中抛出{main}
答案 0 :(得分:1)
bindValue()
调用中使用的参数名称必须与查询中使用的参数名称匹配
所以
$sql = "insert into register
(name,user_name,email_id,password)
values (:name, :username, :email, :password)";
$query = $this->db->pdo->prepare($sql);
$query->bindValue(':name',$name);
$query->bindValue(':user_name',$usnm);
// The FIX
//$query->bindValue(':email_id',$email);
$query->bindValue(':email',$email);
$query->bindValue(':password',$pass);
$result = $query->execute();
或者,更改查询参数
$sql = "insert into register
(name,user_name,email_id,password)
values (:name, :username, :email_id, :password)";
// ^^^^^^^^^
$query = $this->db->pdo->prepare($sql);
$query->bindValue(':name',$name);
$query->bindValue(':user_name',$usnm);
$query->bindValue(':email_id',$email);
$query->bindValue(':password',$pass);
$result = $query->execute();
这也是你的下一个问题,一个缺少的WHERE,你在绑定
时犯了同样的错误
public function emailcheck($email){
$sql = "select email_id from register WHERE email_id = :email";
// ^^^^^
$query = $this->db->pdo->prepare($sql);
// AND THE SAME FIX REQUIRED HERE
//$query->bindparam(':email_id', $email);
$query->bindValue(':email',$email);
$query->execute();
答案 1 :(得分:1)
我是因为在为参数分配值时的拼写错误。 这应该工作。在#47行修改您的查询
$sql = "insert into register (name,user_name,email_id,password) values (:name, :user_name, :email_id, :password)";