【问题标题】:Cant find why my code started displaying Trying to get property of non-object suddenly找不到为什么我的代码开始显示尝试突然获取非对象的属性
【发布时间】: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-&gt;num_rows; 任何帮助表示赞赏..谢谢你

已编辑片段我已经删除了' 下面dave 告诉的双重'...这导致空值成功输入 Db 但是当我尝试输入电子邮件值时..它说数据无法插入..即在register.php中插入值到db的查询sql1失败时的错误

【问题讨论】:

  • 试图获取非对象的属性 什么?你已经在有趣的地方剪掉了通知
  • 哦,对不起..我的大脑都搞砸了..我已经编辑了我的代码..请看看@AlanMachado

标签: php mysql mysqli


【解决方案1】:

嗯,你有这个:

$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();
    }

【讨论】:

  • 哇..我认为这是 M 出错的地方..你能告诉我解决这个问题的方法吗?我已将我的电子邮件设置为接受 Null 值,并且它的默认值在 phpmyadmin 中也是 null ..但它从不接受 null 值..所以我选择了这种方式..感谢您尝试帮助我@dave
  • 因此,您的插入语句也是错误的,您在更新时使用SET,在插入时您指定列然后是值。我这样做的方式使用bind_param 来防止SQL 注入,我使用func_num_args 是为了方便 - 如果您在该行中添加更多列,您只需将其添加到函数签名中,然后在插入中添加列名称,一切都会正常工作
  • 它给出了这个致命错误.. Cannot use [] for reading in D:\wamp\www\blood\include\class.user.php on line 31 并且第 31 行是 $columns[]; @dave
  • 嘿,戴夫,非常感谢您抽出宝贵的时间。我已经尝试了你上面给出的内容。但它给了我 2 个错误。就像 ( ! ) Warning: call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in D:\wamp\www\blood\include\class.user.php on line 42 和 ( ! ) Fatal error: Call to a member function execute() on a non-object in D:\wamp\www\blood\include\class.user.php on line 43 看起来像你最后错过了}..请帮助我..@dave
猜你喜欢
  • 1970-01-01
  • 2019-05-19
  • 2021-02-10
  • 2019-07-08
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
相关资源
最近更新 更多