[英]Login script not working
Here is the complete script: 这是完整的脚本:
connection.php connection.php
class Connection{
public function dbConnect(){
return new PDO("mysql:host=localhost; dbname=test", "root", "");
}
}
user.php user.php的
include_once('connection.php'); include_once( 'connection.php');
class User{
private $db;
public function __constructor(){
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function Login($name, $pass){
if(!empty($name) && !empty($pass)){
$st = $this->db->prepare("SELECT * FROM users WHERE name =? and pass =?");
$st->bindParam(1, $name);
$st->bindParam(2, $pass);
$st->execute();
if($st->rowCount() == 1){
echo "User verified. Access granted";
}else{
echo "Incorrect";
}
}else{
echo "Please enter name and password";
}
}
}
index.php 的index.php
include_once('User.php');
if(isset($_POST['submit'])){
$name = $_POST['user'];
$pass = $_POST['pass'];
$object = new User();
$object->Login($name, $pass);
}
But when I try it, this error appear: 但是当我尝试它时,会出现以下错误:
Fatal error: Call to a member function prepare() on a non-object in C:\\wamp\\www\\testes\\User.php on line 16 致命错误:在第16行的C:\\ wamp \\ www \\ testes \\ User.php中的非对象上调用成员函数prepare()
Line 16: $st = $this->db->prepare("SELECT * FROM users WHERE name =? and pass =?"); 第16行:$ st = $ this-> db-> prepare(“SELECT * FROM users WHERE name =?and pass =?”);
Your magic is busted 你的魔法被破坏了
public function __constructor(){
should be 应该
public function __construct(){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.