php - 尝试将登录页面从 mysql_connect 更改为使用 PDO,但没有成功

标签 php mysql pdo

我正在尝试将登录页面从 mysql_connect(工作完美)更改为使用 PDO,但没有成功。
每次我使用正确的用户名和密码点击“登录”时,它都会刷新相同的登录页面。

提前致谢!

使用 mysql_connect 的工作代码:

<? 
    session_start();
    $user = "XXXX";
    $password = "YYYY";
?>

<!DOCTYPE html>

<html>
  <head>
    <title>Home</title>
  </head>
  <body>
    <br><br><br><br><br><br><br><br>
    <div align="center"><h1>Home</h1>
    <h3>
      <? if (isset($_SESSION["authenticated"])) { ?>
        You are logged in!  
        <br />
        <a href="logout.php">log out</a>
        <a href="userpage.php">See page</a>
      <? } else { ?>
        You are not logged in!
      <? } ?>
    </h3>
    <br>
    <?
        if (($connection = mysql_connect("localhost", $user, $password)) === false)
            die("Could not connect to database");

        // select database
        if (mysql_select_db("123456", $connection) === false)
            die("Could not select database");

        // if username and password were submitted, check them
        if (isset($_POST["name"]) && isset($_POST["password"]))
        {
            // prepare SQL
            $sql = sprintf("SELECT * FROM students WHERE name='%s'",
                           mysql_real_escape_string($_POST["name"]));

            // execute query
            $result = mysql_query($sql);
            if ($result === false)
                die("Could not query database");

            // check whether we found a row
            if (mysql_num_rows($result) == 1)
            {
                // fetch row
                $row = mysql_fetch_assoc($result);

                // check password
                if ($row["password"] == $_POST["password"])
                {
                    // remember that user's logged in
                    $_SESSION["authenticated"] = true;
                    $host = $_SERVER["HTTP_HOST"];
                    $path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
                    header("Location: http://$host$path/userpage.php");
                    exit;
                }
            }
        }
    ?>
    <form action="<?= $_SERVER["PHP_SELF"] ?>" method="post">
      <table>
        <tr>
          <td>Username:</td>
          <td>
            <input name="name" type="text"></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input name="password" type="password"></td>
        </tr>
        <tr>
          <td></td>
          <td><input type="submit" value="Log In"></td>
        </tr>
      </table>      
    </form></div>
  </body>
</html>

带有 PDO 的新代码(不起作用):

<? 
    session_start();
    $user = "XXXX";
    $password = "YYYY";
    $dbh = new PDO('mysql:host=localhost;dbname=123456', $user, $password);
?>

<!DOCTYPE html>

<html>
  <head>
    <title>Home</title>
  </head>
  <body>
    <br><br><br><br><br><br><br><br>
    <div align="center"><h1>Home</h1>
    <h3>
      <? if (isset($_SESSION["authenticated"])) { ?>
        You are logged in!  
        <br />
        <a href="logout.php">log out</a>
        <a href="userpage.php">See page</a>
      <? } else { ?>
        You are not logged in!
      <? } ?>
    </h3>
    <br>
    <?
        // if username and password were submitted, check them
        if (isset($_POST["name"]) && isset($_POST["password"]))
        {
            // prepare SQL
            $idd = $_POST["name"];
            $qry = "SELECT * FROM students WHERE name='$idd'";
            $result = $dbh->query($qry);

            if ($result === false)
                die("Could not query database");

            if (mysql_num_rows($result) === false)
                die("No luck!");

            if (mysql_num_rows($result) == 1)
            {
                // fetch row
                $row = mysql_fetch_assoc($result);

                // check password
                if ($row["password"] == ($_POST["password"]))
                {
                    // remember that user's logged in
                    $_SESSION["authenticated"] = true;
                    $host = $_SERVER["HTTP_HOST"];
                    $path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
                    header("Location: http://$host$path/userpage.php");
                    exit;
                }
            }
        }
    ?>
    <form action="<?= $_SERVER["PHP_SELF"] ?>" method="post">
      <table>
        <tr>
          <td>Username:</td>
          <td>
            <input name="name" type="text"></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input name="password" type="password"></td>
        </tr>
        <tr>
          <td></td>
          <td><input type="submit" value="Log In"></td>
        </tr>
      </table>      
    </form></div>
  </body>
</html>

最佳答案

您正在查询方法返回的 PDOStatement 对象上使用 mysql_* 函数。要获取行数,请尝试 $result->rowCount()。要获取记录,请使用其中一种获取方法。看这个link

关于php - 尝试将登录页面从 mysql_connect 更改为使用 PDO,但没有成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22269503/

相关文章:

php - REST 服务和内存缓存

php - 插入select语句,更改字段和逻辑思维

PHP将嵌套的json保存到mysql数据库

php - 从文件夹中取消链接文件

php - PDO 不能绑定(bind)两个属性

javascript - 允许访客和站点成员使用 ajax、php/pdo、css、html 查看谁在线/登录

php - 从 mySQL 数据库中选择行并显示为列

mysql - 将两个表中的两列合并为一个

php - 按更多组合分组

php - MySQL PDO WHERE 语句中的 NULL