【发布时间】:2020-09-12 01:39:21
【问题描述】:
我正在 localhost 上编写一个网站,当我尝试登录以提交新用户时发现此错误
致命错误:未捕获的错误:在 null 上调用成员函数 quote() 在 C:\xampp\htdocs\recipes\db_manager\users.php:31 堆栈跟踪:#0 C:\xampp\htdocs\recipes\users_manager\checkSubmit.php(15): notExistsUser('usertest') #1 {main} 抛出 C:\xampp\htdocs\recipes\db_manager\users.php 在第 31 行
checkSubmit.php:
<?php
session_start();
include_once("../db_manager/common.php");
if(isset($_POST["username"]) && isset($_POST["pwd"])) {
$username = $_POST["username"];
$pwd = $_POST["pwd"];
if(notExistsUser($username)){
submit($username, $pwd);
redirect("../index.php", "User successfully submitted!");
} else {
redirect("../submit.php", "This username already exists");
}
}
?>
users.php 中的函数 notExistsUser
function notExistsUser($username) {
$db = attachdb();
$user = $db->quote($username); //Line 31
$rows = $db->query("SELECT * FROM Users WHERE username = $user");
if($rows->rowCount() > 0)
return false;
else
return true;
}
函数 attachdb()
function attachdb() {
$add = 'mysql:dbname=Recipes;host=localhost';
try {
$db = new PDO($add, 'root', 'mysql');
return $db;
} catch (PDOException $ex) {
?>
<p>A database error occurred!.</p>
<?php
return NULL;
}
}
这个错误是什么意思,我该如何解决?
【问题讨论】:
-
你能显示
attachdb()的代码吗?