php - 尝试设置安全登录我在第 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}
解决方案
推荐阅读
- asp.net-core - 在 ASP.NET Core 3.1 中使用配置中的 ConnectionString
- javascript - 如何使用 Cloud Functions、Node.js 和 Javascript 对 Firebase 实时数据库进行排序和过滤以获得“高分”排行榜
- php - 如何使用 eloquent laravel 在数据库中保存标签数组列表
- python - 滑动垂直和水平标记 matplotlib
- teamcity - TeamCity 多个构建代理问题
- arrays - 将不在另一个数组matlab中的值放入数组中
- javascript - 仅当用户键入内容时如何验证注册
- openid-connect - Angular8、ASP.NET Core Api、OpenIdConnect 和 IdentityServer4 我的 NG 应用无法获取 /.well-known(...) 文档甚至无法获取 ErrorMessage
- c# - DataGridView 最小列宽
- python - Monitor variable in a loop with VSCode debugger