很抱歉发布这个问题..我已经被这段代码困了很长时间突然开始显示Trying to property of non-object
通知..我添加了$email = !empty($email) ? "'$email'": "NULL";
当没有在电子邮件列中输入数据时,我的代码中的这一行用于向我的数据库输入空值..这就是 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;}
}
?>
注册.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;
感谢任何帮助..谢谢
edited piece 我已经删除了下面 dave
所说的双 '
...这导致空值成功进入Db 但是当我尝试输入电子邮件值时..它说无法插入数据..即,在 register.php 中将值插入 db 的查询 sql1
失败时的错误
最佳答案
好吧,你有这个:
$email = !empty($email) ? "'$email'" : "NULL";
$sql="SELECT * FROM users WHERE username='$username' OR email='$email'";
所以,让我们假设 $email = "something@gmail.com"; $username = "whatever"
所以你有
$email = !empty("something@gmail.com") ? "'something@gmail.com'" : "NULL";
然后你有
$sql="SELECT * FROM users WHERE username='whatever' OR email=''something@gmail.com''";
所以,你的查询是
SELECT * FROM users WHERE username='whatever' OR email=''something@gmail.com''
双 '
使其成为无效查询,因此当您运行 query($sql)
时它返回 false
$count_row = (false)->num_rows;
显然会抛出一个错误。
我会把它改成:
public function reg_user($firstname,$lastname,$bloodgroup,$dob,$country,$state,$district,$city,$phonenumber,$secondnumber,$email,$username,$password,$activity){
$password = md5($password);
$email = $email ?: null;
$sql="SELECT * FROM users WHERE username=? OR email=?";
//checking if the username or email is available in db
$stmt = $this->db->prepare($sql);
$stmt->bind_param('ss', $username, $email);
$stmt->execute();
//if the username is not in db then insert to the table
if ($stmt->num_rows == 0) {
$ref = new ReflectionMethod($this, 'reg_user');
$columns = [];
foreach($ref->getParameters() as $param) {
$name = $param->name;
$columns[$name] = &$$name;
}
$sql= "INSERT INTO users
(" . implode(",", array_keys($columns)) . ")
VALUES
(" . str_repeat("?,", count($columns)) . ")";
$stmt = $this->db->prepare($sql);
call_user_func_array(array($stmt, "bind_param"), $columns);
return $stmt->execute();
}
关于php - 无法找到为什么我的代码开始显示试图突然获取非对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32954780/