首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >获取错误严格标准:在第20行的register.php中,只应通过引用传递变量

获取错误严格标准:在第20行的register.php中,只应通过引用传递变量
EN

Stack Overflow用户
提问于 2018-06-16 23:44:20
回答 1查看 18关注 0票数 0

我从GitHub (Link to it)下载了登录php脚本,当我尝试注册时,它会显示这个错误:登录页面上的Strict Standards: Only variables should be passed by reference in /homework/register.php on line 20没有显示任何错误。

我使用的代码如下:

代码语言:javascript
复制
 <?php

    session_start();

    if( isset($_SESSION['user_id']) ){
        header("Location: /");
    }

    require 'database.php';

    $message = '';

    if(!empty($_POST['email']) && !empty($_POST['password'])):

        // Enter the new user in the database
        $sql = "INSERT INTO users (email, password) VALUES (:email, :password)";
        $stmt = $conn->prepare($sql);

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

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

    endif;

    ?>

    <!DOCTYPE html>
    <html>
    <head>
        <title>Register Below</title>
        <link rel="stylesheet" type="text/css" href="assets/css/style.css">
        <link href='http://fonts.googleapis.com/css?family=Comfortaa' rel='stylesheet' type='text/css'>
    </head>
    <body>

        <div class="header">
            <a href="/">Homework</a>
        </div>

        <?php if(!empty($message)): ?>
            <p><?= $message ?></p>
        <?php endif; ?>

        <h1>Register</h1>
        <span>or <a href="login.php">login here</a></span>

        <form action="register.php" method="POST">

            <input type="text" placeholder="Enter your email" name="email">
            <input type="password" placeholder="and password" name="password">
            <input type="password" placeholder="confirm password" name="confirm_password">
            <input type="submit">

        </form>

    </body>
    </html>

database.php (如果需要):

代码语言:javascript
复制
<?php
$server = 'localhost';
$username = 'my_username';
$password = 'my_password';
$database = 'my_dbname';

try{
    $conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch(PDOException $e){
    die( "Connection failed: " . $e->getMessage());
}

数据库工作正常。没有收到错误。

有人能解释一下注册出了什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-16 23:48:49

在将值传递给bind_param()时,您需要传递一个变量,而不是函数的返回值...

代码语言:javascript
复制
$stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));

为了..。

代码语言:javascript
复制
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
$stmt->bindParam(':password', $password);

当您看到前面有一个&的参数时,如&$variable in...

公共布尔PDOStatement::bindParam ( mixed $parameter,mixed &$variable [,int $data_type = PDO::PARAM_STR [,int $length,mixed $driver_options ]] )

您需要传递一个变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50889344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档