【发布时间】:2015-10-05 17:50:42
【问题描述】:
很抱歉发布这个问题..这段代码突然开始显示Trying to get property of non-object通知..我在我的代码中添加了$email = !empty($email) ? "'$email'" : "NULL";这一行,以便在我的代码中输入空值db 当没有在电子邮件列中输入数据时..这就是 M 收到此通知的原因..因此查询
$check = $this->db->query($sql) ;
$count_row = $check->num_rows;
失败,用户可以使用已用于注册的用户名或电子邮件进行注册..我已将他的功能设为公开并将 include_once 更改为 require_once..但仍然没有用..请帮助我..我附上整个代码..请看一下
注意:请不要将其标记为重复,因为我已经阅读了所有可能的相关问题但发现没有用..
class.user.php
<?php
include "db_config.php";
class User{
public $db;
public function __construct(){
$this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if(mysqli_connect_errno()) {
echo "Error: Could not connect to database.";
exit;
}
}
/*** for registration process ***/
public function reg_user($firstname,$lastname,$bloodgroup,$dob,$country,$state,$district,$city,$phonenumber,$secondnumber,$email,$username,$password,$activity){
//$password = crypt($password);
$password = md5($password);
$email = !empty($email) ? "'$email'" : "NULL";
$sql="SELECT * FROM users WHERE username='$username' OR email='$email'";
//checking if the username or email is available in db
$check = $this->db->query($sql) ;
$count_row = $check->num_rows;
//if the username is not in db then insert to the table
if ($count_row == 0){
$sql1="INSERT INTO users SET firstname='$firstname',lastname='$lastname',bloodgroup='$bloodgroup',dob='$dob',
country='$country',state='$state',district='$district',city='$city',phonenumber='$phonenumber',secondnumber='$secondnumber',
email=$email,username='$username', password='$password',activity='$activity'";
$result = mysqli_query($this->db,$sql1) or die(mysqli_connect_errno()."Data cannot inserted");
return $result;
}
else { return false;}
}
?>
register.php
<?php
require_once 'include/class.user.php';
$user = new User();
// Checking for user logged in or not
/*if (!$user->get_session())
{
header("location:index.php");
}*/
if (isset($_REQUEST['submit'])){
extract($_REQUEST);
//$email = !empty($email) ? "'$email'" : "NULL";
$register = $user->reg_user($firstname,$lastname,$bloodgroup,$dob,$country,$state,$district,$city,$phonenumber,$secondnumber,$email,$username,$password,$activity);
if ($register) {
// Registration Success
echo 'Registration successful <a href="login.php">Click here</a> to login';
} else {
//registration failed
echo' registration failed,username or email already exists';
}
}
?>
错误是Trying to get property of non-object in D:\wamp\www\blood\include\class.user.php on line 29
第 29 行是 class.user.php 中的$count_row = $check->num_rows;
任何帮助表示赞赏..谢谢你
已编辑片段我已经删除了' 下面dave 告诉的双重'...这导致空值成功输入 Db 但是当我尝试输入电子邮件值时..它说数据无法插入..即在register.php中插入值到db的查询sql1失败时的错误
【问题讨论】:
-
试图获取非对象的属性 什么?你已经在有趣的地方剪掉了通知
-
哦,对不起..我的大脑都搞砸了..我已经编辑了我的代码..请看看@AlanMachado