<?php
$connection=new PDO("mysql:host=localhost;dbname=userdata", "secure_credentials", "battery_staple");
$user=$_POST['username1'];
$pass=$_POST['password1'];
$snip=mb_substr($user, 0, 3);
$pass=password_hash($pass, PASSWORD_BCRYPT);
$user_query=$connection->prepare("INSERT INTO login (email, password, semod, snippet) VALUES (:email, :password, :semod, :snippet)");
$user_query->bindParam(':email', $user);
$user_query->bindParam(':password', $pass);
$user_query->bindParam(':semod', "false");
$user_query->bindParam(':snippet', $snip);
$user_query->execute;
(密码已更改)
我在上面的 PHP 代码中遇到了一个小问题,每当执行它时,我都会收到此错误:
Fatal error: Cannot pass parameter 2 by reference in [location] on line [##: I shortened the above code... it's the password field with issues]
环顾四周,在不使用变量的情况下直接传递字符串/整数时,这似乎是一个问题。然而,password_hash()返回一个字符串,所以我相信它没有返回一个字符串。这个问题可能是什么问题?
最佳答案
您的错误在这一行:
$user_query->bindParam(':semod', "false");
您需要使用bindValue
。
$user_query->bindValue(':semod', "false");
bindParam
通过引用传递第二个参数,因此它必须是一个变量,而不是文字。
此外,不需要绑定(bind)已知值。您可以轻松地将文字 'false'
字符串添加到您的语句查询中,即
"INSERT INTO login (email, password, semod, snippet) VALUES (:email, :password, 'false', :snippet)"
关于php - 为什么 PHP 的 "password_hash"不返回字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23073055/