2016-12-12 4 views
-5

plsはpdoおよびmysqlを使用してログインおよび登録システムを作成しようとしています。このエラーは私が{"text":SQLSTATE [HY093]:無効なパラメータ番号:バインドされた変数の数がトークンの数と一致しません}} <

<?php 
 

 
class userClass 
 
{ 
 
\t 
 
/* User Login */ 
 
    public function userLogin($email,$password) 
 
    { 
 

 
      $db = getDB(); 
 
      $hash_password= hash('sha256', $password); 
 
      $stmt = $db->prepare("SELECT uid FROM users WHERE email=:email AND password=:hash_password"); 
 
      $stmt->bindParam("email", $email,PDO::PARAM_STR) ; 
 
      $stmt->bindParam("hash_password", $hash_password,PDO::PARAM_STR) ; 
 
      $stmt->execute(); 
 
      $count=$stmt->rowCount(); 
 
      $data=$stmt->fetch(PDO::FETCH_OBJ); 
 
      $db = null; 
 
      if($count) 
 
      { 
 
       $_SESSION['uid']=$data->uid; 
 
       return true; 
 
      } 
 
      else 
 
      { 
 
       return false; 
 
      }  
 
    } 
 

 
    /* User Registration */ 
 
    public function userRegistration($email,$password,$fname,$lname,$dob,$sex,$country,$state_resd,$phone_no,$profile_pic) 
 
    { 
 
      try{ 
 
      $db = getDB(); 
 
      $st = $db->prepare("SELECT uid FROM users WHERE email=:email, password=:hash_password, fname=:fname, lname=:lname, dob=:dob, sex=:sex, country=:country, state_resd=:state_resd, phone_no=:phone_no, profile_pic=:profile_pic"); 
 
      /**$st->bindParam("username", $username,PDO::PARAM_STR);**/ 
 
      $st->bindParam("email", $email,PDO::PARAM_STR); 
 
      $st->execute(); 
 
      $count=$st->rowCount(); 
 
      if($count<1) 
 
      { 
 
      $stmt = $db->prepare("INSERT INTO users(email,password,fname,lname,dob,sex,country,state_resd,phone_no,profile_pic) VALUES (:email,:hash_password:fname,:lname,:dob,:sex,:country,:state_resd,:phone_no)"); 
 
      /**$stmt->bindParam("username", $username,PDO::PARAM_STR) ;**/ 
 
      
 
      $stmt->bindParam("email", $email,PDO::PARAM_STR) ; 
 
\t $hash_password= hash('sha256', $password); 
 
      $stmt->bindParam("hash_password", $hash_password,PDO::PARAM_STR) ; 
 
      $stmt->bindParam("fname", $fname,PDO::PARAM_STR) ; 
 
\t $stmt->bindParam("lname", $lname,PDO::PARAM_STR) ; 
 
\t $stmt->bindParam("dob", $dob,PDO::PARAM_STR) ; 
 
\t $stmt->bindParam("sex", $sex,PDO::PARAM_STR) ; 
 
\t $stmt->bindParam("country", $country,PDO::PARAM_STR) ; 
 
\t $stmt->bindParam("state_resd", $state_resd,PDO::PARAM_STR) ; 
 
\t $stmt->bindParam("phone_no", $phone_no,PDO::PARAM_STR) ; 
 
\t $stmt->bindParam("profile_pic", $profile_pic,PDO::PARAM_STR) ; 
 
\t 
 
      $stmt->execute(); 
 
      $uid=$db->lastInsertId(); 
 
      $db = null; 
 
      $_SESSION['uid']=$uid; 
 
      return true; 
 

 
      } 
 
      else 
 
      { 
 
      $db = null; 
 
      return false; 
 
      } 
 
      
 
      
 
      } 
 
      catch(PDOException $e) { 
 
      echo '{"error":{"text":'. $e->getMessage() .'}}'; 
 
      } 
 
    } 
 
     
 
    /* User Details */ 
 
    public function userDetails($uid) 
 
    { 
 
     try{ 
 
      $db = getDB(); 
 
      $stmt = $db->prepare("SELECT email FROM users WHERE uid=:uid"); 
 
      $stmt->bindParam("uid", $uid,PDO::PARAM_INT); 
 
      $stmt->execute(); 
 
      $data = $stmt->fetch(PDO::FETCH_OBJ); 
 
      return $data; 
 
     } 
 
     catch(PDOException $e) { 
 
      echo '{"error":{"text":'. $e->getMessage() .'}}'; 
 
      } 
 

 
    } 
 

 

 
} 
 
?>

と登録ページ

のために...この種のエラーのすべての答えを通して読みが、地雷を是正するように見えるカント... HERESにコード、ポップアップ続けます

<?php 
 
include("config.php"); 
 
include('class/userClass.php'); 
 
$userClass = new userClass(); 
 

 
$errorMsgReg=''; 
 
$errorMsgLogin=''; 
 
if (!empty($_POST['loginSubmit'])) 
 
{ 
 
$email=$_POST['email']; 
 
$password=$_POST['password']; 
 

 
if(strlen(trim($email))>1 && strlen(trim($password))>1) 
 
    { 
 
    $uid=$userClass->userLogin($email,$password); 
 
    if($uid) 
 
    { 
 
     $url=BASE_URL.'home.php'; 
 
     header("Location: $url"); 
 
    } 
 
    else 
 
    { 
 
     $errorMsgLogin="Please check login details."; 
 
    } 
 
    } 
 
} 
 

 
if (!empty($_POST['signupSubmit'])) 
 
{ 
 

 
\t /**$username=$_POST['usernameReg'];**/ 
 
\t 
 
\t $email=$_POST['emailReg']; 
 
\t $password=$_POST['passwordReg']; 
 
    $fname=$_POST['fnameReg']; 
 
    $lname=$_POST['lnameReg']; 
 
    $dob=$_POST['dobReg']; 
 
    $sex=$_POST['sexReg']; 
 
    $country=$_POST['countryReg']; 
 
    $state_resd=$_POST['state_resdReg']; 
 
    $phone_no=$_POST['phone_noReg']; 
 
    $profile_pic=$_POST['profile_picReg']; 
 
\t /**$username_check = preg_match('~^[A-Za-z0-9_]{3,20}$~i', $username);**/ 
 
\t $email_check = preg_match('~^[a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$~i', $email); 
 
\t $password_check = preg_match('~^[[email protected]#$%^&*()_]{6,20}$~i', $password); 
 

 
\t if($email_check && $password_check && strlen(trim($fname))>0) 
 
\t { 
 
    $uid=$userClass->userRegistration($email,$password,$fname,$lname,$dob,$sex,$country,$state_resd,$phone_no,$profile_pic); 
 
    if($uid) 
 
    { 
 
    \t $url=BASE_URL.'home.php'; 
 
    \t header("Location: $url"); 
 
    } 
 
    else 
 
    { 
 
     $errorMsgReg="Email already exits."; 
 
    } 
 
    
 
\t } 
 

 

 
} 
 

 
?> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<style> 
 
#container{width: 700px} 
 
#login,#signup{width: 300px; border: 1px solid #d6d7da; padding: 0px 15px 15px 15px; border-radius: 5px;font-family: arial; line-height: 16px;color: #333333; font-size: 14px; background: #ffffff;rgba(200,200,200,0.7) 0 4px 10px -1px} 
 
#login{float:left;} 
 
#signup{float:right;} 
 
h3{color:#365D98} 
 
form label{font-weight: bold;} 
 
form label, form input{display: block;margin-bottom: 5px;width: 90%} 
 
form input{ border: solid 1px #666666;padding: 10px;border: solid 1px #BDC7D8; margin-bottom: 20px} 
 
.button { 
 
    background-color: #5fcf80 !important; 
 
    border-color: #3ac162 !important; 
 
    font-weight: bold; 
 
    padding: 12px 15px; 
 
    max-width: 300px; 
 
    color: #ffffff; 
 
} 
 
.errorMsg{color: #cc0000;margin-bottom: 10px} 
 
</style> 
 
<body> 
 
<div id="container"> 
 
<<div id="signup"> 
 
<h3>Registration</h3> 
 
<form method="post" action="" name="signup"> 
 
<label>Email</label> 
 
<input type="text" name="emailReg" autocomplete="off" /> 
 
<label>Password</label> 
 
<input type="password" name="passwordReg" autocomplete="off"/> 
 

 
<label>First Name</label> 
 
<input type="text" name="fnameReg" autocomplete="off" /> 
 
<label>Last Name</label> 
 
<input type="text" name="lnameReg" autocomplete="off" /> 
 

 
<label>Sex</label> 
 
<input type="text" name="sexReg" autocomplete="off" /> 
 

 

 
<label>Date of Birth</label> 
 
<input type="text" name="dobReg" autocomplete="off" /> 
 
<label>Country</label> 
 
<input type="text" name="countryReg" autocomplete="off" /> 
 

 
<label>State of Residence</label> 
 
<input type="text" name="state_resdReg" autocomplete="off" /> 
 

 
<label>Phone Number</label> 
 
<input type="text" name="phone_noReg" autocomplete="off" /> 
 
<label>Profile photo</label> 
 
<input type="text" name="profile_picReg" autocomplete="off" /> 
 
<!--<label>Username</label> 
 
<input type="text" name="usernameReg" autocomplete="off" />--> 
 

 

 
<div class="errorMsg"><?php echo $errorMsgReg; ?></div> 
 
<input type="submit" class="button" name="signupSubmit" value="Create Counter"> 
 
<a href="index.php" class="to_register"> Login </a> 
 
</form> 
 
</div> 
 

 

 

 

 
</div> 
 

 
</body> 
 
</html>

PLSは自明であるあなたが取得している初心者

+0

はあなたが間違っている...ウルレジスタ機能に一つだけのパラメータを結合しているたくさんあります:)

/* User Login */ public function userLogin($email,$password) { $db = getDB(); // $hash_password= hash('sha256', $password); $stmt = $db->prepare("SELECT uid,email,password FROM users WHERE email=:email"); $stmt->bindParam(":email", $email,PDO::PARAM_STR) ; $stmt->execute(); $results= $stmt->fetchall(PDO::FETCH_ASSOC); if(count($results) > 0){ foreach($results as $row){ if(password_verify($password,$row['password'])){ $_SESSION['uid']=$data->uid; return true; //$password is the password from the user }else{ //provided password does not match stored hash return false; } } }else{ //No results return false; } } 

希望が、私はそこに任意の中括弧を逃さなかったが、あなたが持っている:のようになります。多くのプレースホルダ –

+0

あなたのinsert文も間違っています.. –

+1

ここでPDOの正しい使い方をお読みください:https://phpdelusions.net/pdo –

答えて

0

エラーに近い午前として私が間違ってやっているかを見るために私のコードを見てみましょう助け、それはあなたがパラメータの数であることを意味しますバインディングは、Queryステートメントの最初の部分で使用する列名の数と等しくありません。

あなたのレジスタの機能は、この

/* User Registration */ 
    public function userRegistration($email,$password,$fname,$lname,$dob,$sex,$country,$state_resd,$phone_no,$profile_pic) 
    { 
      try{ 
      $db = getDB(); 
      $st = $db->prepare("SELECT uid FROM users WHERE email=:email LIMIT 1"); 
      $st->bindParam("email", $email,PDO::PARAM_STR); 
      $st->execute(); 
      $count=$st->rowCount(); 
      if($count<1) 
      { 
      $stmt = $db->prepare("INSERT INTO users(email,password,fname,lname,dob,sex,country,state_resd,phone_no,profile_pic) VALUES (:email,:hash_password,:fname,:lname,:dob,:sex,:country,:state_resd,:phone_no,:profile_pic)"); 

      $stmt->bindParam("email", $email,PDO::PARAM_STR) ; 
     $hash_password= password_hash($password,PASSWORD_DEFAULT); 
      $stmt->bindParam(":hash_password", $hash_password,PDO::PARAM_STR) ; 
      $stmt->bindParam(":fname", $fname,PDO::PARAM_STR) ; 
     $stmt->bindParam(":lname", $lname,PDO::PARAM_STR) ; 
     $stmt->bindParam(":dob", $dob,PDO::PARAM_STR) ; 
     $stmt->bindParam(":sex", $sex,PDO::PARAM_STR) ; 
     $stmt->bindParam(":country", $country,PDO::PARAM_STR) ; 
     $stmt->bindParam(":state_resd", $state_resd,PDO::PARAM_STR) ; 
     $stmt->bindParam(":phone_no", $phone_no,PDO::PARAM_STR) ; 
     $stmt->bindParam(":profile_pic", $profile_pic,PDO::PARAM_STR) ; 

      $stmt->execute(); 
      $uid=$db->lastInsertId(); 
      $db = null; 
      $_SESSION['uid']=$uid; 
      return true; 

      } 
      else 
      { 
      $db = null; 
      return false; 
      } 


      } 
      catch(PDOException $e) { 
      echo '{"error":{"text":'. $e->getMessage() .'}}'; 
      } 
    } 

NBのようになります。あなたがあなたのパスワードをハッシュしていると思ったとき、私はあなたが使用していたSHA256から変更されています。私はpassword_hash();password_verify();を使用しました。 Secure hash and salt for PHP passwords

サイトにアクセスする必要があるかもしれません。 https://phpdelusions.net/pdoを参照してください。

オルタナティブ/ PDOを使用して挿入する通常の簡単な方法:

$stmt = $db->prepare("INSERT INTO users(email,password,fname,lname,dob,sex,country,state_resd,phone_no,profile_pic) VALUES (?,?,?,?,?,?,?,?,?,?)"); 
$stmt->execute(array($email,$hash_password,$fname,$lname,$dob,$sex,$country,$state_resd,$phone_no,$profile_pic)); 

あなたのログイン機能password_verify使用して();

+0

中括弧を紛失したことはありませんでした。私は本当にあなたの答えをありがとう..私は送信した訂正コードを使用しましたが、このエラーメッセージが表示されます。致命的なエラー:57行目のC:\ Wamp \ www \ appyam \ class \ userClass.phpの未定義の関数password_hash()を呼び出してください。ちょうどphp5.3を使用してpassword_compatをダウンロードしました – JerryCole

+0

ああ、問題はあなたのPHPバージョンで5.6以上が必要です –

+0

ありがとうございました...私は5.7にアップグレードしました。ありがとう – JerryCole

関連する問題