dongxu2398 2016-10-29 04:51
浏览 28
已采纳

Mysql PDO转换验证

I am completely new to PDO and I am trying to convert my register page, I am able to get the info in the database but I fell I am making it more complicated then it needs to be. DO I need the 2 sets of POST in each function?

also my echo validation errors conflicts with the EXCEPTION in the create function.

I have a register function and I call it by using the following on my register page

<?php validate_register();?>

This is my validate register code

/* 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");
            }
        }
    }
}

and if the validation passes it creates user

/* 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条回答 默认 最新

  • douke1905 2016-10-29 07:25
    关注

    You don't have to reassign the $_POST again in create_user since you are passing them into the function from the validate_register function:

    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();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line