1

I got this error when i run this method . can any one explain me what's wrong in the code . Below is my function code

public function storeUser($name, $email, $password) {
    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt

    $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
    $stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
    $stmt->execute();
    $result=$stmt->bind_result($uuid, $name, $email, $encrypted_password, $salt);
   // $result = $stmt->execute();
    $stmt->close();

    // check for successful store
    if ($result) {
        $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $user = $stmt->fetch();
        $stmt->close();

        return $user;
    } else {
        return false;
    }
}
9
  • You really shouldn't use your own salts on password hashes and you really should use PHP's built-in functions to handle password security. If you're using a PHP version less than 5.5 you can use the password_hash() compatibility pack. Apr 8, 2016 at 17:23
  • @JayBlanchard can you me any simple example of it Apr 8, 2016 at 17:25
  • There are links in the comments which demonstrate the concepts. Apr 8, 2016 at 17:26
  • 1
    Why are you doing a ->bind_result() on an INSERT? Your INSERT query does not return any results to bind. It should be used on a SELECT
    – Sean
    Apr 8, 2016 at 17:29
  • 1
    It could be that, if the unique_id column is an AI (int), then you can't bind it on INSERT, AI will do that on its own. Or, you need to use an i instead of an s and doing VALUES('', ?, ?, ?, ?, Apr 8, 2016 at 17:30

1 Answer 1

1

The erro says that you have bind N variables but none fields, because it's an insert does not return value. bind_result() should use in selects statements.

To fix, remove this line:

$result=$stmt->bind_result($uuid, $name, $email, $encrypted_password, $salt);
5
  • I've done result binding on INSERT before, so this may not be the complete fix. Apr 8, 2016 at 17:43
  • @Fred-ii- i'll do a double but i think this fix the problem, 'for work' the insert should return 6 columns LOL i know is very very very weird xD
    – rray
    Apr 8, 2016 at 17:47
  • @rray No problemo. FYI: result binding can be done for an SELECT/INSERT, as I've written a script just last week as a matter of fact. It's tricky, but it can be done. ;-) Cheers Apr 8, 2016 at 17:48
  • @Fred-ii- could you pass some referece? i never see it, in postgres is possible an insert return the 'auto-increment'
    – rray
    Apr 8, 2016 at 17:50
  • @rray You can insert into an AI'd column using a prepared statement, just as long as the syntax (in MySQL) is (ai'd_column, col2, col3) VALUES ('', ?, ?) type of thing. Apr 8, 2016 at 17:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.