sql >> データベース >  >> RDS >> Mysql

ユーザー名が存在するかどうかを確認するPDO

     <?php
    //Connections
    try {
        $handler = new PDO('mysql:host=localhost;dbname=s','root', '*');
        $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e){
        exit($e->getMessage());
    }
    
    $name = $_POST['name']; 
    $username = $_POST['username']; 
    $email = $_POST['email'];   
    $password = $_POST['password']; 
    $password1 = $_POST['passwordconf'];
    $ip = $_SERVER['REMOTE_ADDR'];
    
    
    //Verifcation 
    if (empty($name) || empty($username) || empty($email) || empty($password) || empty($password1)){
        $error = "Complete all fields";
    }
    
    // Password match
    if ($password != $password1){
        $error = "Passwords don't match";
    }
    
    // Email validation
    
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $error = "Enter a  valid email";
    }
    
    // Password length
    if (strlen($password) <= 6){
        $error = "Choose a password longer then 6 character";
    }
    
    if(!isset($error)){
    //no error
    $sthandler = $handler->prepare("SELECT username FROM users WHERE username = :name");
    $sthandler->bindParam(':name', $username);
    $sthandler->execute();
    
    if($sthandler->rowCount() > 0){
        echo "exists! cannot insert";
    } else {
        //Securly insert into database
        $sql = 'INSERT INTO userinfo (name ,username, email, password, ip) VALUES (:name,:username,:email,:password,:ip)';    
        $query = $handler->prepare($sql);
    
        $query->execute(array(
    
        ':name' => $name,
        ':username' => $username,
        ':email' => $email,
        ':password' => $password,
        ':ip' => $ip
    
        ));
        }
    }else{
        echo "error occured: ".$error;
        exit();
    }
    


    1. SQL Serverのdatetime2とsmalldatetime:違いは何ですか?

    2. ORA-03115を取得:anonymous pl/sqlからvarcharの配列をフェッチ中にサポートされていないネットワークデータ型または表現エラー

    3. PostgreSQLで月名を月番号に変換する

    4. 内部にカウントがあるmysqlgroup_concat?