Php 未定义的属性
您好,我正在创建一个登录脚本,但由于某些原因,我在调用Php 未定义的属性,php,mysqli,Php,Mysqli,您好,我正在创建一个登录脚本,但由于某些原因,我在调用$mysqli时遇到了一个未定义的属性错误,但是我在其他函数上以相同的方式调用了它,它工作得很好,例如,我调用它的方式是so$this->mysqli->prepare()这对我的插入函数有效 然而,当在real\u escape\u string()的实例中使用它时,我实际上遇到了两个错误: 注意:第28行的/Applications/XAMPP/xamppfiles/htdocs/imanage/class.Login.php中未定义的属
$mysqli
时遇到了一个未定义的属性错误,但是我在其他函数上以相同的方式调用了它,它工作得很好,例如,我调用它的方式是so$this->mysqli->prepare()代码>这对我的插入函数有效
然而,当在real\u escape\u string()的实例中使用它时,我实际上遇到了两个错误:
注意:第28行的/Applications/XAMPP/xamppfiles/htdocs/imanage/class.Login.php中未定义的属性:Login:$mysqli
致命错误:在第28行的/Applications/XAMPP/xamppfiles/htdocs/imanage/class.Login.php中对非对象调用成员函数real_escape_string()
编辑:第28行指的是这一行
$safeUser=$this->mysqli->real\u escape\u string($user)代码>
我真的不明白我做了什么不同的事情,有什么办法可以解决这个问题吗
如有任何意见和帮助,将不胜感激
我的代码如下:
index.php
<div id="maincontentWrapper">
<div id="maincontent">
<div id="contentWrapper"></div><!--End loginWrapper -->
<article>
<p>Welcome to iManage, please login in below.</p>
</article>
<div id="loginform">
<div id="loginWrapper">
<form id="loginForm" method="POST" action="class.Login.php">
<h1><span class="log-in">Log in</span> or <span class="sign-up"><a href="register">sign up</a></span></h1>
<div id="errorDiv"><?php
if (isset($_SESSION['error']) & isset($_SESSION['formAttempt'])) {
unset($_SESSION['formAttempt']);
print "Errors encountered<br/>\n";
foreach ($_SESSION['error'] as $error) {
print $error . "<br />\n";
} //end foreach
} //end if
?></div>
<p class="float">
<label for="login"><i class="icon-user"></i>Username</label>
<input type="text" id="email" name="email" placeholder="E-mail">
<span class="errorFeedback errorSpan" id="emailError">E-mail is required</span>
</p>
<p class="float">
<label for="password"><i class="icon-lock"></i>Password</label>
<input type="password" id="password" name="password" placeholder="Password" class="showpassword">
<span class="errorFeedback errorSpan" id="passwordError">Password is required</span>
</p>
<p class="clearfix">
<input type="submit" name="submit" value="Log in"></form>
</p>
</div>
</div>
</div>
</div>
</div>
用户名
电子邮件是必需的
密码
密码是必需的
class.Login.php
<?php
include("connect/class.Connect.php");
class Login extends Database {
public $id;
public $email;
public $username;
function __construct() {
if (session_id() == "") {
session_start();
}
if (isset ($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn'] == true) {
$this->_initUser();
}
} // end construct
public function authenticate($user, $pass) {
$user = isset($_POST['email']);
$pass = isset($_POST['password']);
$safeUser = $this->mysqli->real_escape_string($user);
$incomingPassword = $this->mysqli->real_escape_string($pass);
$query = "SELECT * from users WHERE email = '{$safeUser}'";
if (!$result = $this->mysqli->query($query)) {
error_log("Cannot retrieve account for {$user}");
return false;
}
// will be only one row, so no while() loop needed
$row = $result->fetch_assoc();
$dbPassword = $row['password'];
if (crypt($incomingPassword,$dbPassword) != $dbPassword) {
error_log("Passwords for {$user} don't match");
return false;
}
$this->id = $row['id'];
$this->username = $row['username'];
$this->email = $row['email'];
$this->isLoggedIn = true;
$this->_setSession();
return true;
} // end authenticate
private function _setSession() {
if (session_id() == '') {
session_start();
}
$_SESSION['id'] = $this->id;
$_SESSION['email'] = $this->email;
$_SESSION['username'] = $this->username;
$_SESSION['isLoggedIn'] = $this->isLoggedIn;
} // end function setSession
private function _initUser() {
if (session_id() == '') {
session_start();
}
$this->id = $_SESSION['id'];
$this->email = $row['email'];
$this->username = $row['username'];
$this->user_role = $row['user_role'];
$this->isLoggedIn = $_SESSION['isLoggedIn'];
} // end initUser
function preventaccess () {
if (!isset($_POST['submit'])) {
die(header("Location: login.php"));
}
} // end prevent access
function validatelogin () {
$_SESSION['formAttempt'] = true;
if (isset($_SESSION['error'])) {
unset($_SESSION['error']);
}
$_SESSION['error'] = array();
$required = array("email", "password");
//Check required fields
foreach ($required as $requiredField) {
if (!isset($_POST[$requiredField]) || $_POST[$requiredField] == "") {
$_SESSION['error'][] = $requiredField . " is required.";
}
}
if (!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
$_SESSION['error'][] = "Invalid e-mail address";
}
if (count($_SESSION['error']) > 0) {
die(header("Location: login.php"));
} else {
$user = new User;
if ($user->authenciate($_POST['email'], $_POST['password'])) {
unset($_SESSION['formAttempt']);
die(header("Location: authenticated.php"));
}else {
$_SESSION['error'][] = "There was a problem with your username or password.";
die(header("Location: login.php"));
}
}
} // end validate
}
$run = new Login();
$run->__construct();
$run->authenticate($_POST['email'],$_POST['password']);
$run->validatelogin();
?>
您的数据库类中没有定义属性mysqli
。这应该可以
<?php
/**
* MySQLi database
*/
class Database {
private $_mysqli;
public function __construct() {
$this->_mysqli = new mysqli('localhost', 'root', '', 'imanage');
if(mysqli_connect_errno()) {
echo "Error: Could not connect to database.";
exit;
}
/*else{
echo"Your Database successfully connected";
}*/
}
public function __destruct(){
$this->_mysqli->close();
}
}
?>
类登录扩展数据库{
################
...
函数_u构造(){
...
父::u-construct();如果您能准确指出第28行是什么,那将很有帮助。@AndyLester抱歉,我编辑了我的问题来反映这一太多的代码,导致了这样一个常见的错误。我建议关闭。至于您的错误原因:您没有调用父类的构造函数:parent:u-construct()
-但无论如何,您的代码中存在一些问题。如果您与他人分享您以这种方式编写/设计代码的原因,甚至可以给您一些好的提示,不仅仅是如何删除警告。如果您想有效地了解这些内容,您应该删除代码中的一些实际问题。如果您对代码审阅感兴趣,请知道;但是如果我在调用我的数据库时使用$this->mysqli
我获得了成功的交互,为什么在这种情况下会有什么不同?你能不能也用一个例子来编辑你的答案,说明你将如何定义属性mysqli
pleasethanks,那么当我查询数据库时,我将如何调用它?目前我正在使用$this->mysqli->prepare()
在您的情况下,我会使用$this->\u mysqli
吗?还有为什么我的mysqli
在准备好的语句的情况下工作,而在上面的示例中不工作?如果父构造函数设置了该属性,您至少需要调用父构造函数一次才能设置该属性。计算机程序真的很笨,它只在至少,您已经写入了它。如果您没有调用父构造函数,脚本将无法自行修复它。@hakre我不是通过使用以下行调用它的吗$run->\uu construct();
很可能不是。构造函数不是这样调用的。可以通过new ClassName();
或parent::\uu construct())
但不像你刚才评论的那样。添加这个仍然没有什么不同。好吧,错误必须消失,否则你仍然在做错事。但是正如我前面评论的那样(这就是旋转),即使错误消失,您的代码中仍然存在问题。简言之:您的问题应该关闭,因为它不适合此网站。您必须提出一个具体的编程问题,如“帮助”部分所述。您看到了什么类型的问题?知道会有帮助当然,您很渴望。但请仔细阅读。我已经告诉过您了到目前为止,还有两次问题。即使你很好奇,你也不能提出不同的问题吗?
<?php
/**
* MySQLi database
*/
class Database {
private $_mysqli;
public function __construct() {
$this->_mysqli = new mysqli('localhost', 'root', '', 'imanage');
if(mysqli_connect_errno()) {
echo "Error: Could not connect to database.";
exit;
}
/*else{
echo"Your Database successfully connected";
}*/
}
public function __destruct(){
$this->_mysqli->close();
}
}
?>
class Login extends Database {
################
...
function __construct() {
...
parent::__construct(); <--- missing
######################
...
} // end construct
...