在null

时间:2018-12-06 08:05:34

标签: php

为学校项目创建用户创建页面。

使用localhost时,代码工作正常,但是将标题上载到网络主机后出现了错误。

错误出现在“ $ stmt-> close();”上,我认为没有被调用,但不确定为什么它在我的系统上内部起作用。

<?php

/* entering localhost config instead
require_once "config.php"; 
*/

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'users');

$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}

$username = $password = $confirm_password = "";
$username_err = $password_err = $confirm_password_err = "";

if($_SERVER["REQUEST_METHOD"] == "POST"){

if(empty(trim($_POST["username"]))){
    $username_err = "Please enter a username.";
} else{
    $sql = "SELECT id FROM users WHERE username = ?";

    if($stmt = $mysqli->prepare($sql)){
        $stmt->bind_param("s", $param_username);
        $param_username = trim($_POST["username"]);

        if($stmt->execute()){
            $stmt->store_result();

            if($stmt->num_rows == 1){
                $username_err = "This username is already taken.";
            } else{
                $username = trim($_POST["username"]);
            }
        } else{
            echo "Something went wrong.";
        }
    }

    $stmt->close();
}

if(empty(trim($_POST["password"]))){
    $password_err = "Please enter a password.";     
} elseif(strlen(trim($_POST["password"])) < 6){
    $password_err = "Password must have atleast 6 characters.";
} else{
    $password = trim($_POST["password"]);
}

if(empty(trim($_POST["confirm_password"]))){
    $confirm_password_err = "Please confirm password.";     
} else{
    $confirm_password = trim($_POST["confirm_password"]);
    if(empty($password_err) && ($password != $confirm_password)){
        $confirm_password_err = "Password did not match.";
    }
}

if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){

    $sql = "INSERT INTO users (username, password) VALUES (?, ?)";

    if($stmt = $mysqli->prepare($sql)){
        $stmt->bind_param("ss", $param_username, $param_password);

        $param_username = $username;
        $param_password = password_hash($password, PASSWORD_DEFAULT); 

        if($stmt->execute()){
            header("location: login.php");
        } else{
            echo "Something went wrong.";
        }
    }

    $stmt->close();
}

$mysqli->close();
}
?>

已解决

哑巴错误

已使用

declare(strict_types=1); 
error_reporting(-1); 
ini_set('display_errors', 'true');
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

发现它只是db配置文件中的一个字符错误。抱歉浪费您的时间。

顺便说一句-密码在数据库中散列了。

1 个答案:

答案 0 :(得分:0)

您写道:class int { set (val) { this = val | 0; // Truncate } get () { return this; } } var x = new int(); x = 5 / 2; console.log(x); // shows 2 instead of 2.5 如果条件不匹配,$ sdmtp将为null,因此您的if($stmt = $mysqli->prepare($sql)){将失败。

将行$stmt->close();移动到if执行块中。

您面临的另一个问题是您对代码确实在开发系统(非本地主机)上产生此错误的事实感到困惑。此迁移是由开发系统上$stmt->close();中的异常引起的。