【问题标题】:i want to pass my teacher_id as session variable [closed]我想将我的teacher_id 作为会话变量传递[关闭]
【发布时间】:2020-06-08 19:57:54
【问题描述】:

我想在登录后将teacher_id 作为会话变量传递。在 loginteacher.php 中,我已将用户电子邮件作为会话变量传递。

loginteacher.php

<?php
session_start();
//Database Configuration File
include('database.php');
error_reporting(1);
if (isset($_POST['submit'])) {
    // Getting username/ email and password

    $email=$_POST['email'];
    $password=$_POST['password'];
    $teacher_id = $_POST['teacher_id'];

    // Fetch data from database on the basis of username/email and password

    $sql ="SELECT teacher_id, email ,password FROM teacher WHERE (email=:email and teacher_id=:teacher_id)";
    $query= $conn -> prepare($sql);

    $query-> bindParam(':email', $email,':teacher_id', $teacher_id, PDO::PARAM_STR);
    $query-> execute();
    $results=$query->fetchAll(PDO::FETCH_OBJ);
    if ($query->rowCount() > 0) {
        foreach ($results as $row) {
            $passwordhash = $row->password;

        }

        //verifying Password
        $pass =password_verify($password,$passwordhash);

        if ($pass) 
        {
            $_SESSION['email']=$_POST['email'];
            $_SESSION['teacher_id']=$_POST['teacher_id'];

            header('location:http://localhost/kridha/teacherdashboard.php');
        }
        else 
        {
            echo "<script type='text/javascript'>alert('wrong credtials');
            window.location='teacher.html';
            </script>";
        }

    }
//if username or email not found in database
    else{
        echo "<script type='text/javascript'>alert('User not registered with us');
            window.location='teacher.html';
            </script>";
    }
}
?>

这里我已经回显了会话 ID。

teacherdashboard.php

<?php 
session_start();

if(!isset($_SESSION['email']))
{
    // not logged in
    header('Location: teacher.html');
    exit();
}
echo $_SESSION['teacher_id'];
?>

但它显示了这个错误

致命错误:未捕获的 PDOException:SQLSTATE[42000]:语法错误或 访问冲突:1064 您的 SQL 语法有错误;检查 与您的 MariaDB 服务器版本相对应的手册 在第 1 行的 ':email 和 teacher_id=:teacher_id' 附近使用的语法 C:\xampp\htdocs\kridha\loginteacher.php:22
堆栈跟踪:
#0 C:\xampp\htdocs\kridha\loginteacher.php(22): PDOStatement->execute()
#1 {main}
在第 22 行的 C:\xampp\htdocs\kridha\loginteacher.php 中抛出

【问题讨论】:

  • 问题与会话变量无关,是MySQL的问题。
  • @Barmar They asked this already。在另一个帐户下。我认为你在浪费时间。

标签: php mysql pdo


【解决方案1】:

您不能在一次调用bindParam 中绑定多个参数。您必须为每个参数单独调用bindParam()

$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->bindParam(':teacher_id', $teacher_id, PDO::PARAM_STR);

或者,您可以将所有参数作为数组参数传递给execute()

$query->execute([':email' => $email, ':teacher_id' => $teacher_id]);

但是从后来的 cmets 来看,teacher_id 不是您表单中的输入之一,因此您不应该在 WHERE 子句中使用它。应该就是这样。

    $sql ="SELECT teacher_id, email ,password FROM teacher WHERE email=:email";
    $query= $conn -> prepare($sql);

    $query-> bindParam(':email', $email, PDO::PARAM_STR);

【讨论】:

  • 我已经按照你说的做了......但现在它显示//if username or email not found in database else{ echo "&lt;script type='text/javascript'&gt;alert('User not registered with us'); window.location='teacher.html'; &lt;/script&gt;"; }
  • 听起来你有一个额外的 ?&gt; 不属于它。
  • 你不应该在浏览器中看到 PHP 源代码。见stackoverflow.com/questions/5121495/…
  • 不不......我是说警报弹出窗口正在显示......它直接进入外部其他部分,即user is not registerd us
  • 那么电子邮件或教师 ID 与数据库中的不匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 2016-12-19
  • 2014-02-25
  • 1970-01-01
  • 1970-01-01
  • 2011-06-12
相关资源
最近更新 更多