【问题标题】:Php strict standards: Only variables should be passed by reference [duplicate]php严格标准:只有变量应该通过引用传递[重复]
【发布时间】:2016-10-05 10:02:47
【问题描述】:

所以我的代码如下所示:

$sql = "INSERT INTO users (email, password) VALUES (:email, :password)";
$stmt = $conn->prepare($sql);

$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':password', sha1($_POST['password']));

if( $stmt->execute() ):
$message = 'Successfully created new user';
else:
$message = 'Sorry there must have been an issue creating your account';
endif; 

错误是由这行引起的:

$stmt->bindParam(':password', sha1($_POST['password']));

希望有人可以帮助我删除“严格标准:仅应通过引用传递变量”错误。因为它仍在执行一切。

【问题讨论】:

  • $pass =sha1($_POST['password']); $stmt->bindParam(':password', $pass);
  • 是的,最好在传递给另一个函数参数之前格式化你的变量。

标签: php


【解决方案1】:

bindParam 引用第二个参数而不是值。这样做是为了在执行语句之前对变量值的更改被识别,或者换个说法,所以使用绑定变量的值在查询的执行时,而不是变量的值绑定的时候。

引用仅适用于变量 - 您不能将引用传递给函数调用。如果您将函数调用用作 bindParam 的第二个参数,则传递的是值而不是引用,这就是一切正常工作的原因 - 但它首先违背了使用引用的目的。

修复错误信息:

$passSha1 = sha1($_POST['password'])
$stmt->bindParam(':password', $passSha1);

// if you change passSha1 here, the new value will be used later
// in the execution of the statement

if( $stmt->execute() ): 
// ...

【讨论】:

  • 另一种解决方案是使用 bindValue() 代替。它的行为与 OP 所期望的 bindParam() 完全一样。
【解决方案2】:

您是否尝试过提取变量?像这样的:

$passwordHash = sha1($_POST['password']);
$stmt->bindParam(':password', $passwordHash);

【讨论】:

    猜你喜欢
    • 2014-11-01
    • 2015-05-27
    • 1970-01-01
    • 2014-08-17
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多