douao2000 2014-06-10 18:46
浏览 28
已采纳

Php PDO与数据库的连接

I have database with 2 table. Students and Profesors. I create one more login table and there are all email and passwords from students and profesors. I want to create that Student try to login that will send him on some student.php and when profesor try to login it will send him on profesor.php

I try with this code, but it always return me 1. So this is not good options..

if(isset($_POST['Submit'])){

$sql= "SELECT COUNT (*) FROM `students` AND 'Profesors' WHERE `username` = :username and `password` = :password ";
$result = $connection->prepare($sql);
$result->bindParam(":username" ,$_POST['username']);
$result->bindParam(":password" ,$_POST['password']);
$result->execute();

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

I need some idea, how to change my datebase or this login code.

  • 写回答

4条回答 默认 最新

  • douhao2856 2014-06-10 19:36
    关注

    I would personally not let professors and student use the same login. Now to answer the question I would change my query to the following:

    SELECT user_type FROM `login_table` 
    WHERE `username` = :username AND `password` = :password 
    

    once query return result I would check the user_type field and redirect accordingly.

    $num=$result->rowCount();
    if($num > 0){
       $data=$result->fetch();
       if($data['professor']){
          header("location: professor.php");
       }else{
          header("location: student.php");
       }
    }else{
        header("location:login.php");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么