-2

I am getting a error while running below code any one here pls help me out

public function storeUser($name, $email, $password, $phone,$stream) {

   $response = array();

    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt

    if (!$this->isUserExisted($email)) {

        $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, phone,stream, salt, created_at) VALUES('$uuid','$name','$email',$'$encrypted_password','$phone','$stream','$salt',NOW())");
        $stmt->bind_param("isssisss", $uuid,$name,$email,$encrypted_password,$phone,$stream,$salt,NOW());

        $result = $stmt->execute();

        $stmt->close();

        if ($result) {

            // User successfully inserted
            $response["error"] = false;
            $response["user"] = $this->getUserByEmailAndPassword($email);
        }else {

            $response["error"] = true;
            $response["message"] = "Oops! An error occurred while registereing";
        }


    } else {

        $response["error"] = false;
        $response["user"] = $this->getUserByEmailAndPassword($email);
    }

      return $response;

}

the error is

PHP Fatal error: Call to a member function bind_param() on a non-object in /var/www/html/android_login_api_test/include/DB_Functions.php on line 98

Sjon
  • 4,989
  • 6
  • 28
  • 46
  • It means your SQL query failed / is invalid. Try running it through PHPMyAdmin to see what happened. Also, your VALUES should be question marks if those are what you are binding values to through bind_param. – Homberto Apr 11 '16 at 18:38
  • Replace the variable name in the query to `?` and remove `NOW()` from the parameters in `bind_param` – Alon Eitan Apr 11 '16 at 18:38
  • 1
    I take it you didn't bother reading and understanding exactly "how" prepared statements work http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php – Funk Forty Niner Apr 11 '16 at 18:40

2 Answers2

3

You're preparing a query without placeholders, i.e.:

$stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, phone,stream, salt, created_at) VALUES('$uuid','$name','$email',$'$encrypted_password','$phone','$stream','$salt',NOW())");
$stmt->bind_param("isssisss", $uuid,$name,$email,$encrypted_password,$phone,$stream,$salt,NOW());

It seems to me you copy-pasted the code. Replace the code above with:

$stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, phone,stream, salt, created_at) VALUES(?,?,?,?,?,?,?,NOW())");
$stmt->bind_param("isssiss", $uuid,$name,$email,$encrypted_password,$phone,$stream,$salt);

Note I dropped the last parameter in the bind_param function, and the last 's' in the first argument of said function.

Eduardo Galván
  • 962
  • 7
  • 15
-2

$stmt stores result of prepare function. Seems like prepare doesn't return object, but you trying to access $stmt as an object.

Factory Girl
  • 910
  • 4
  • 18
  • 29