dongxi0523 2012-03-22 09:39
浏览 17

使用函数和PDO检查帐户是否存在

I am having some trouble using functions to check if user account already exist in the backend. I created two functions: one to check if user account already exist and the other to create account.

Can someone enlighten me...? What is wrong with the code here?

<?php

    try{
    $username = 'web';
    $password = '1234';
    $username_signup = $_POST['username_signup'];
    $password_signup = $_POST['password_signup'];
    $hash = crypt($_POST['password'], '$3a$08$2'); // salt 

    $connection = new PDO ('mysql:host=localhost;dbname=tongue', $username, $password);
    $connection -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $connection -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    function check(c){
    $statement = $connection->prepare('
        SELECT email FROM user
        WHERE email=:username;
        ');
    $statement -> bindParam(':username', c, PDO::PARAM_STR, 127);
    $check = $statement -> execute();
    return $check;
    };

    function create(a,b){
    $statement = $connection->prepare('
        INSERT INTO user (email, hash)
        VALUES (:username, :hash);
        ');
    $statement -> bindParam(':username', a, PDO::PARAM_STR, 127);
    $statement -> bindParam(':hash', b, PDO::PARAM_STR);
    $statement -> execute();
    }

    check($username_signup);

    if ($check==0){
        create($username_signup, $hash);
        header("Location=index.php");
        exit();
    } else {
        header("Location=sign_up.php?error=1");
        exit();
    }


    $connection = null; 
    } // try{}

    catch(PDOException $e) {
        echo $e->getMessage();
        }   

?>
  • 写回答

2条回答 默认 最新

  • dongzhuo1958 2012-03-22 09:44
    关注

    $connection is not global so it is not set inside the functions so you either need to make it global (don't) or pass it in as a argument

    p.s. you should really work on your functions and variables names

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么