无法找到我的代码开始显示的原因试图突然获取非对象的属性

时间:2015-10-05 17:50:43

标签: php mysql mysqli

抱歉发布这个问题。我已经被这段代码卡住了很长时间,突然开始显示Trying to get property of non-object通知..我在我的代码中添加了$email = !empty($email) ? "'$email'" : "NULL";在电子邮件列中没有输入数据时,我的数据库为空值。这就是M得到此通知的原因。因为这个查询

$check =  $this->db->query($sql) ;
            $count_row = $check->num_rows;  

失败,用户可以注册已经用于注册的用户名或电子邮件。我已将其功能公开,并将include_once更改为require_once ..但仍然没有用..请帮助我.. m附上整个代码。好好看看

注意请不要将其标记为重复,因为我已阅读所有可能的相关问题,但未找到任何用途..

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失败时的错误

1 个答案:

答案 0 :(得分:3)

嗯,你有这个:

$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''

'使其成为无效查询,因此当您运行false时会返回query($sql)

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