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

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况