首页 > 解决方案 > 注册 PHP sql 失败

问题描述

基本上我正在制作一个 PHP 注册脚本,但我得到“SQL 失败”,但所有细节都是正确的

我的代码

$link = mysqli_connect("localhost", "username", "password");
$database = mysqli_select_db($link, "database");

$user = mysqli_real_escape_string($link, $_GET['username']);
$password = mysqli_real_escape_string($link, $_GET['password']);

$table = "login";

$sql = "SELECT * FROM " . $table . " WHERE username=?";
$stmt = mysqli_stmt_init($link);

if (!mysqli_stmt_prepare($stmt, $sql))
{
    echo "SQL Failed.";
} else {

    mysqli_stmt_bind_param($stmt, "s", $user);
    mysqli_stmt_execute($stmt);
    Signupuser($user, $password, $table, $link);
}

function Signupuser($user, $password, $table, $link)
{
    $options = ['cost' => 11];

    $hashedPassword = password_hash($password, PASSWORD_BCRYPT, $options);

    $sql = "INSERT INTO `" . $table . "` (username, password) VALUES (?, ?);";
    $stmt = mysqli_stmt_init($link);

    if (!mysqli_stmt_prepare($stmt, $sql))
    {
        echo "SQL Failed.";
    } else {

        mysqli_stmt_bind_param($stmt, "ss", $user, $hashedPassword);
        mysqli_stmt_execute($stmt);

        echo "User Created!";
    }
}

但这会导致 SQL 失败,但所有细节都正确?难道我做错了什么...?

任何帮助和/或指示将非常感谢!

标签: phpmysqlmysqli

解决方案


推荐阅读