douao2000 2014-06-10 10: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..

  1. if(isset($_POST['Submit'])){
  2. $sql= "SELECT COUNT (*) FROM `students` AND 'Profesors' WHERE `username` = :username and `password` = :password ";
  3. $result = $connection->prepare($sql);
  4. $result->bindParam(":username" ,$_POST['username']);
  5. $result->bindParam(":password" ,$_POST['password']);
  6. $result->execute();
  7. $num=$result->rowCount();
  8. if($num > 0){
  9. header("location:index.php");
  10. }else{
  11. header("location:login.php");
  12. }

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

  • 写回答

4条回答 默认 最新

  • douhao2856 2014-06-10 11: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 爱快路由器端口更改错误导致无法访问
  • ¥20 安装catkin时遇到了如下问题请问该如何解决呢
  • ¥15 VAE模型如何输出结果
  • ¥15 编译python程序为pyd文件报错:{"source code string cannot contain null bytes"
  • ¥20 关于#r语言#的问题:广义加行模型拟合曲线后如何求拐点
  • ¥15 fluent设置了自动保存后,会有几个时间点不保存
  • ¥20 激光照射到四象线探测器,通过液晶屏显示X、Y值
  • ¥50 数据库开发问题求解答
  • ¥15 安装anaconda时报错
  • ¥15 小程序有个导出到插件方式,我是在分包下引入的插件,这个export的路径对吗,我看官方文档上写的是相对路径
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部