SIGNUP.PHP/HTML
<?php
require_once("connections/db.php");
$error = array();
if($user->is_loggedin())
{
$user->redirect('index.php');
}
if(isset($_POST['signup-btn']))
{
$username = $_POST['signup-username'];
$password = $_POST['signup-password'];
$email = $_POST['signup-email'];
if($user='')
{
$error[] = "Please enter a username";
}
else if($pass='')
{
$error[] = "Please enter a password";
}
else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$error[] = "Please enter a valid email address";
}
else
{
try
{
if($user->register($username, $password, $email))
{
echo "registered";
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
?>
class.user.php
<?php
class USER{
private $db;
function __construct($db_con)
{
$this->db = $db_con;
}
public function register($username,$password,$email)
{
try
{
$protected_password = password_hash($upass, PASSWORD_DEFAULT);
$stmt = $this->db->prepare("INSERT INTO users(username,password,email)
VALUES(:username, :password, :email)");
$stmt->bindparam(":username", $username);
$stmt->bindparam(":password", $protected_password);
$stmt->bindparam(":email", $email);
$stmt->execute();
return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function is_loggedin()
{
if (isset($_SESSION['user_session']))
{
return true;
}
}
public function redirect($url)
{
header("Location: $url");
}
public function logout()
{
session_destroy();
unset($_SESSION['user_session']);
return true;
}
}
?>
db.php中
<?php
session_start();
$connection = parse_ini_file('config.ini');
try{
$db_con = new PDO("mysql:host={$connection['host']}; dbname={$connection['dbname']}", $connection['username'], $connection['password']);
$db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo $e->getMessage();
}
include_once 'class.user.php';
$user = new USER($db_con);
?>
我一直收到错误致命错误:在第32行的C:\ xampp \ we \ signup.php中调用字符串上的成员函数register()
我对PDO完全陌生。我无法理解为什么这不起作用。 我尝试尽可能多地更改代码,但此错误并未发生变化。我确定这是一个我忽视的简单愚蠢的问题。
答案 0 :(得分:1)
我们开始从您的表单
设置$username
$username = $_POST['signup-username'];
然后我假设对这个变量($username == ''
?)进行了等式检查,但是将$user
设置为空字符串。
if($user='')
{
$error[] = "Please enter a username";
}
之后,系统会调用$user->register()
,$user
仍然是字符串,而不是您的班级USER
的实例。
请注意,如果您想在其他地方进行平等检查,请不要进行变量分配!我一直这样做。
if($user='')
{
$error[] = "Please enter a username";
}
else if($pass='') // should this be $password == ''?
{
$error[] = "Please enter a password";
}