gpt4 book ai didi

php - 可捕获的 fatal error : Object of class PDO could not be converted to string on line 35

转载 作者:行者123 更新时间:2023-11-29 12:17:18 26 4
gpt4 key购买 nike

我正在尝试以用户身份登录,但不知道会出现此错误,但是当我第二次尝试时,我将可以访问,但登录后同样如此,并且在第二次尝试登录后,我无法从数据库获取电子邮件它似乎没有创建 session 。它只是传递代码而不创建 session 。

if (!isset($_SESSION['email']) && isset($_POST['email'])) {
if (!empty($_POST['email']) && !empty($_POST['password'])) {

$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
$password=sha1($password);

try {
$dbh = new PDO("mysql:host=$hostname; dbname=$database", $username, $pass);
$dbh -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

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

$stmt = $dbh -> prepare("SELECT email, password FROM tbl_user WHERE email = '".$email."' and password = '".$password."'");
$stmt -> bindParam('".$email."', $email, PDO::PARAM_STR);
$stmt -> bindParam('".$password."', $password, PDO::PARAM_STR);
$stmt -> EXECUTE();
$em = $stmt -> fetchColumn();

if ($em == true) {
// session_register("email");
// session_register("password");

$_SESSION['email'] = $_POST['email'];
$_SESSION['START'] = time();

setcookie("username", $_POST['email'], mktime()+(60*3), "/");
setcookie("LUS", time(), mktime()+(60*3), "/");

$stmt -> $dbh -> prepare("SELECT Name FROM tbl_user WHERE email = '".$email."'");
$stmt -> EXECUTE();

$em2 = $stmt -> fetchColumn();
echo "Logged in.";
} else {
echo "email or password is incorrect.";
}
} catch (Exception $e) {
echo "".$e->getMessage();
}
} elseif (empty($_POST['email']) && !empty($_POST['password'])) {
# code...
echo "Error : Enter your E-mail.";
} elseif (!empty($_POST['email']) && empty($_POST['password'])) {
# code...
echo "Error: Enter your Password";
} else {
echo "Error: Enter your E-mail & Password";
}

}
elseif (isset($_SESSION['email'])) {
# code...
echo "Welcome again you still logged in <strong>" .round((time() . $_SESSION['START'])/60) ."</strong> minutes(s) ago <a href='logout.php'>LogOut</a>";
}
elseif (!isset($_SESSION['email'])) {
# code...
echo "You must loggin first.";
//header('location:../index.php');
}

最佳答案

更改第二个准备好的语句用法

  $stmt -> $dbh -> prepare("SELECT Name FROM tbl_user WHERE email = '".$email."'");

  $stmt = $dbh -> prepare("SELECT Name FROM tbl_user WHERE email = '".$email."'");

关于php - 可捕获的 fatal error : Object of class PDO could not be converted to string on line 35,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29597770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com