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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀