首页 > 解决方案 > 尝试设置安全登录我在第 38 行不断收到错误

问题描述

我正在尝试进行安全登录或注册工作。对我来说不容易,一个业余爱好者!

我一直在尝试按照此处的说明进行操作:

在我的笔记本电脑上,我正在运行 apache2。错误日志告诉我:

[2020 年 5 月 16 日星期六 09:22:25.271736] [php7:error] [pid 974] [client 127.0.0.1:40112] PHP 致命错误:未捕获的错误:在 /var/www 中的字符串上调用成员函数 prepare() /html/includes/userClass.php:38\n堆栈跟踪:\n#0 /var/www/html/index.php(44): userClass->userRegistration('Pedroski55', 'George22', 'pedroski@163. ...', 'Peter Rodgers')\n#1 {main}\n 在第 38 行的 /var/www/html/includes/userClass.php 中抛出,引用者: http://localhost/

这应该让我登录到 allstudentsdb。我没有收到错误消息:

<?php
// connect to my mysql database allstudentsdb
try
{
$pdo = new PDO('mysql:host=localhost;dbname=allstudentsdb', 'myusername', 'mypassword', array(PDO::MYSQL_ATTR_LOCAL_INFILE => true,));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
return $pdo;
}
catch (PDOException $e)
{
$error = 'Unable to connect to the database server' . $e ;
include 'error.html.php';
exit();
}
?>

我看不出问题出在哪里:请问各位专家有什么建议吗?

33 /* User Registration */
34 public function userRegistration($username,$password,$email,$name)
35 {
36try{
37 $db = $_SERVER['DOCUMENT_ROOT'] . '/includes/studentdbReadfrom.inc.php';
38 $st = $db->prepare("SELECT uid FROM neil_users WHERE username=:username OR email=:email");
39 $st->bindParam("username", $username,PDO::PARAM_STR);
40 $st->bindParam("email", $email,PDO::PARAM_STR);
41 $st->execute();
42 $count=$st->rowCount();
43 if($count<1)
44{
45 $stmt = $db->prepare("INSERT INTO neil_users(username,password,email,name) VALUES  (:username,:hash_password,:email,:name)");
46 $stmt->bindParam("username", $username,PDO::PARAM_STR) ;
47 $hash_password= password_hash($_POST['password'], PASSWORD_DEFAULT); //Password encryption 
48 $stmt->bindParam("hash_password", $hash_password,PDO::PARAM_STR) ;
49 $stmt->bindParam("email", $email,PDO::PARAM_STR) ;
50 $stmt->bindParam("name", $name,PDO::PARAM_STR) ;
51 $stmt->execute();
52 $uid=$db->lastInsertId(); // Last inserted row id
53$db = null;
54 $_SESSION['uid']=$uid;
55 return true;
56 }
57 else
58{
59 $db = null;
60 return false;
61}
62 
63 }
64 catch(PDOException $e) {
65 echo '{"error":{"text":'. $e->getMessage() .'}}';
66 }
67}

标签: php

解决方案


推荐阅读