duanqin2026 2013-11-16 22:27
浏览 13

无法使用PHP和PDO记录用户

From the PDO manual:

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

I have this code:

<?php

require 'core.inc.php';

if(isset($_POST['user']) AND isset($_POST['pass'])){

    if(!empty($_POST['user']) AND !empty($_POST['pass'])){
        $user = $_POST['user'];
        $pass = $_POST['pass'];
            require 'connect.inc.php';
            $st = $db_pdo->prepare("SELECT id FROM users WHERE user=? AND pass=?");
            $st->bindParam(1, $user);
            $st->bindParam(2, $pass);
            $st->execute();
            $result = $st->fetchColumn();
                if($st->rowCount() > 0){
                   $id_arr = $st->fetch();
                   $id = $id_arr[0];
                   $_SESSION['user_id'] = $id;
                   header('Location: edit.php');
                    } else { echo 'Username or password incorrect';}

    } else { echo 'You must fill in all fields.';}
}
?>

The number of rows returned is always 0, as stated in the manual. No surprise there.

1) I want to log a user in and set his $_SESSION id to his user ID from a db.

2) I suck so much at PDO it's not even funny, I can't figure out what function to use to log the user in. If the number or rows is always 0, clearly the Username or password incorrect will appear all the time.

  • 写回答

1条回答 默认 最新

  • douhan8009 2013-11-17 01:59
    关注

    Try this :

    try 
    
     {
    $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
    $bdd = new PDO('mysql:host='.$host.';dbname=' . $dbname, $dbuser, $dbpassword, $pdo_options);
    
    $request = $bdd->prepare('SELECT id FROM users WHERE user = :user AND pass = :password');
    $request->execute(array('user' => $_POST['user'],                                               
                            'password' => $_POST['pass']                  
                                                        ));
    
    $data = $request->fetch();
    
    if ($data)
        {
             session_start();
             $_SESSION['ID'] = $data['id'];
             //you can run other verification here if you want
             header('Location: edit.php');
    
        }
    else 
        { 
            echo 'Username or password incorrect';
        }
    $requete->closeCursor();
    
    }
    
    catch(Exception $erreur)
    
       {
          die('Login error : '.$erreur->getMessage());
       }
    

    Hope that helps .

    评论

报告相同问题?

悬赏问题

  • ¥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的速度时间图像)我想问线路信息是什么