Php严格标准:只能通过引用传递变量

Php严格标准:只能通过引用传递变量,php,Php,因此,我的代码如下所示: $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

因此,我的代码如下所示:

$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; 
$passwordHash = sha1($_POST['password']);
$stmt->bindParam(':password', $passwordHash);
如果错误是由此行引起的:

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

希望有人能帮我消除“严格标准:只有变量应该通过引用传递”的错误。因为它仍在执行所有操作。

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

$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; 
$passwordHash = sha1($_POST['password']);
$stmt->bindParam(':password', $passwordHash);

你试过提取一个变量吗?大概是这样的:

$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; 
$passwordHash = sha1($_POST['password']);
$stmt->bindParam(':password', $passwordHash);

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() ): 
// ...

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() ): 
// ...

$pass=sha1($_POST['password'])$stmt->bindParam(':password',$pass);是的,最好在将变量传递到另一个函数参数之前格式化变量。$pass=sha1($\u POST['password'])$stmt->bindParam(':password',$pass);是的,在传递到另一个函数参数之前,最好先格式化变量。另一种解决方案是使用bindValue()。它的行为与OP期望的bindParam()完全相同。另一种解决方案是使用bindValue()。它的行为与OP所期望的bindParam()完全相同。