2016-10-29 4 views
0

私はPDOを完全に新しくしており、登録ページを変換しようとしています。 データベースの情報を取得できましたが、私はそれをもっと複雑にしています。それぞれの機能にPOSTの2セットが必要ですか?Mysql PDO変換の有効性

私のエコー検証エラーは、作成機能のEXCEPTIONと競合します。

私はレジスタ機能を持っていると私は私のレジスタページ

<?php validate_register();?> 

で次の使用してそれを呼び出すこれは私の検証・レジスタ・コード

/* Validate register */ 

function validate_register(){ 

    $errors = []; 

    $min = 3; 
    $max = 20; 

    if(isset($_POST['signupBtn'])){  

     $email = $_POST['email']; 
     $username = $_POST['username']; 
     $birthdate = $_POST['birthdate']; 
     $password = $_POST['password']; 

     if (empty($email)) { 
      $errors[] = "Email Address Required."; 
     } 

     if (empty($username)) { 
      $errors[] = "Userame Required."; 
     } 

     if (empty($birthdate)) { 
      $errors[] = "Date of Birth Required."; 
     } 

     if (empty($password)) { 
      $errors[] = "Password Required."; 
     } 

     if (! empty($errors)) { 

      echo validation_errors($errors[0]); 

     } else { 

      if (create_user($email, $username, $birthdate, $password)) { 

       set_message('Please check your email for account Information.'); 

       redirect ("index.php"); 
      } 
     } 
    } 
} 

で、検証が合格した場合には、ユーザー

を作成します
/* create user */ 

function create_user($email, $username, $birthdate, $password){ 

    $email = $_POST['email']; 
    $username = $_POST['username']; 
    $birthdate = $_POST['birthdate']; 
    $password = $_POST['password']; 

    $hashed_password = password_hash($password, PASSWORD_DEFAULT); 


    try { 

     $sqlInsert = "INSERT INTO users (email, username, birthdate, password) 
        VALUES(:email, :username, :birthdate, :password)"; 

     $stmt = $db->prepare($sqlInsert); 
     $stmt->execute(array(':email' =>$email, ':username' => $username, ':birthdate' => $birthdate, ':password' => $hashed_password)); 

     if ($stmt->rowCount() == 1) { 

      return true; 
     } 


    }catch (PDOException $e){ 

     $result = $e->getMessage(); 
    } 
} 

答えて

1

を再度0123に再割り当てする必要はありませんあなたはvalidate_register関数から関数に渡すされているので:

validate_register()

function validate_register(){ 
    $errors = []; 
    $min = 3; 
    $max = 20; 

    if(isset($_POST['signupBtn'])){  
     # Just setting here is fine like you have 
     $email = $_POST['email']; 
     $username = $_POST['username']; 
     $birthdate = $_POST['birthdate']; 
     $password = $_POST['password']; 

    ...etc. 

CREATE_USER()説明のため

function create_user($email, $username, $birthdate, $password){ 
    ################################################################ 
    # As noted, remove this section because you have set them in the 
    # parameters and passed them from the previous function. As long 
    # as they are in the same order when you pass them, you're good 
    ################################################################ 

    $hashed_password = password_hash($password, PASSWORD_DEFAULT); 

    try { 

     $sqlInsert = "INSERT INTO users (email, username, birthdate, password) 
        VALUES(:email, :username, :birthdate, :password)"; 

     $stmt = $db->prepare($sqlInsert); 
     $stmt->execute(array(':email' =>$email, ':username' => $username, ':birthdate' => $birthdate, ':password' => $hashed_password)); 

     if ($stmt->rowCount() == 1) { 
      return true; 
     } 
    }catch (PDOException $e){ 

     $result = $e->getMessage(); 
    } 
} 
+0

おかげで、しかしどこを参照検証は? mysqliで私は検証がエラーをエコーし​​たが、PDOはPDOExceptionエラーを必要とするようだ。 – Case

+0

PDOExceptionは、SQLの形式が誤っている場合や、mysqlクエリでその他の問題が発生した場合です。あなたが持っている妥当性検査は、値のためにうまくいくようです。 – Rasclatt

+0

また、 'try' /' catch'は必ずしも必要ではありません。ログに報告するのは良いことですが、結果エラーを直接ユーザーに表示したくない場合もあります。 – Rasclatt