pdo错误消息弹出错误

时间:2013-05-30 14:26:33

标签: php pdo dreamweaver

好吧所以我刚接触pdo并制作了一个简单的登录系统,但是当用户将他们的用户名或密码留空时却无法收到错误消息但是我没有运气我通过研究和帮助得到了这个代码来自其他人,但我现在很难过。任何帮助都会很棒,请记住我是新手。

P.S我知道这不是一个安全的系统

这是代码

<?php
    session_start();
    //connection String
    $connection = new PDO("sqlsrv:server=server;Database=database", "username", "password"); 

    //Seelcting function
    $smt = $connection->prepare("select user_id, username from account where username = :username and password =:password");

    //setting values to textboxes
    $username = $_POST["txt_username"];
    $password = $_POST["txt_password"];

    //binding values
    $smt->bindParam(':username', $username);
    $smt->bindParam(':password', $password);

    //execution
    $smt->execute();

    //fetching data
    $row = $smt->fetch( PDO::FETCH_ASSOC ) ;  
    echo "$row[user_id]\n\n";
    echo "$row[username]\n\n";
    $_SESSION{"user_id"} = $row["user_id"];



if ($smt->recordCount() == 0){
  $_SESSION['errormsg'] = "login failed";
  header( "location: login.php");
} else{
  header("location: homepage.php");
}


if( isset( $_SESSION['errormsg'] ) ) {
  // do the output
  echo $_SESSION['errormsg'];
  // delete the message from the session, so that we show it only once
  unset( $_SESSION['errormsg'] );
}



?>

1 个答案:

答案 0 :(得分:0)

好的,所以看起来您在设置错误后就会立即解决错误。更改顺序,以便检查是否设置了错误消息,然后重定向并显示错误。