使用session和pdo登录表单

时间:2014-10-27 10:43:27

标签: php mysql pdo

我刚刚开始将旧的sql代码转换为pdo,但我这样做有困难,我试图将我的登录脚本转换为pdo格式,这里是转换

<?php
session_start(); // Starting Session
if (isset($_POST['submit'])) 
    {
        try
            {
                // Define $email and $password
                $email      = $_POST['email'];
                $password   = $_POST['password'];

                //Etablishing Connection with Server 
                $dbhost     = "qwe.com";
                $dbname     = "qwe";
                $dbuser     = "qwe";
                $dbpass     = "qwe";

                $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

                $stmt = $conn->prepare("SELECT * FROM register WHERE WHERE `email` = :email and `password` = :password "); 

               $stmt->execute(array(':email' => $_POST['email'],':password'=> $_POST['password']));

                $num=$stmt->fetchColumn();
                if($num > 0)
                    {
                        header("location:dashboard.php");
                    }
                else
                    {
                        header("location:login.php");
                    }
            }       

 catch (Exception $e) 
        {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
        }
  }
?>

页面未被重定向到任何页面,而是被重定向到空白页面

3 个答案:

答案 0 :(得分:3)

这是因为try{}块需要

catch{}

您必须添加它才能显示该错误

像这样添加:

catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

详细了解here

希望它有所帮助! :d

答案 1 :(得分:2)

在try}

之后添加一个catch语句
     catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }

更新尝试

后插入
<?php
session_start(); // Starting Session
if (isset($_POST['submit'])) 
    {
        try
            {
                // Define $email and $password
                $email      = $_POST['email'];
                $password   = $_POST['password'];

                //Etablishing Connection with Server 
                $dbhost     = "qwe.com";
                $dbname     = "qwe";
                $dbuser     = "qwe";
                $dbpass     = "qwe";

                $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

                $stmt = $conn->prepare("SELECT * FROM register WHERE WHERE `email` = :email and `password` = :password "); 

               $stmt->execute(array(':email' => $_POST['email'],':password'=> $_POST['password']));

                $num=$stmt->fetchColumn();
                if($num > 0)
                    {
                        header("location:dashboard.php");
                    }
                else
                    {
                        header("location:login.php");
                    }
            }       

 catch (Exception $e) 
        {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
        }
  }
?>

答案 2 :(得分:1)

您的查询无法准备,因为您的SQL查询中有2 WHERE

  

SELECT * FROM register WHERE WHERE email =:email和password =   :密码

您应该设置连接以抛出错误:

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

检查所有值,而不仅仅是submit

if (isset($_POST['submit'], $_POST['email'], $_POST['password'])) 

完整代码:

<?php
session_start(); // Starting Session
if (isset($_POST['submit'], $_POST['email'], $_POST['password'])) 
    {
        try
        {
            // Define $email and $password
            $email      = $_POST['email'];
            $password   = $_POST['password'];

            //Etablishing Connection with Server 
            $dbhost     = "qwe.com";
            $dbname     = "qwe";
            $dbuser     = "qwe";
            $dbpass     = "qwe";

            $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $sql = "SELECT * FROM register 
                                    WHERE `email` = :email AND
                                          `password` = :password ";

            $stmt = $conn->prepare($sql); 
            $stmt->execute(array(':email' => $_POST['email'],
                                 ':password'=> $_POST['password']));

            $num=$stmt->rowCount();
            if($num > 0){
                header("location:dashboard.php");
            }
            else{
                header("location:login.php");
            }

        }catch (Exception $e) 
        {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
        }
  }
?>