简体   繁体   English

无法使用PHP中的准备好的语句将数据插入数据库,未报告错误

[英]Failing to insert data into database using prepared statement in PHP, no error reported

I'm trying to create a registration form that stores the users entered data into an MySQL database. 我正在尝试创建一个注册表单,用于将用户输入的数据存储到MySQL数据库中。 I was able to get it to work by manually setting the values, but learned that it was best to use prepared statements. 我可以通过手动设置值来使其工作,但了解到最好使用准备好的语句。 This is what my PHP code looks like: 这是我的PHP代码如下所示:

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "accounts";

//Creating a new connection to the database
$connection = new mysqli($servername, $username, $password, $dbname);

//Checking the connection
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

//SQL string used to insert the data into the database
$sql = "INSERT INTO users (name, email, password) VALUES (?, ?, ?)";

$stmt = mysqli_stmt_init($connection);
if (!mysqli_stmt_prepare($stmt, $sql)) {
    echo "Failed";
} else {
    mysqli_stmt_bind_param($stmt, "sss", $_POST["name"], $_POST["email"], $_POST["passowrd"]);
    mysqli_stmt_execute($stmt);
}
?>

And this is the HTML: 这是HTML:

<div id="wrapper">
    <div id="formContent">
        <h3>Complete the following form to register an account:</h3>

        <form class="register" action="registration.php" method="post">

            Email: <input type="email" name="email" required> <br></br>
            Name: <input type="name" name="name" required> <br></br>
            Password: <input type="password" name="password" required> <br></br>
            Confirm Password: <input type="password" name="confirmed_password" required> <br></br>
            <input type="submit" name="submit">

        </form>
    </div>
</div>

The listed code returns no error but the database is not updated. 列出的代码未返回任何错误,但数据库未更新。 I have been busting my head for some time now, so any help is appreciated. 我已经抽空了一段时间,所以对您的帮助表示感谢。

First, you have a typo for password (you have $_POST['passowrd'] ) and second, this is based on the example from the documentation: 首先,您输入password有错字(您有$_POST['passowrd'] ),其次,这是基于文档中的示例:

# Prepare (use the OOP version of this library)
$query  =   $connection->prepare("INSERT INTO users (`name`, `email`, `password`) VALUES (?, ?, ?)");
# Bind parameters and spell "password" correctly
$query->bind_param('sss', $_POST['name'], $_POST['email'], $_POST['password']);
# Execute
$query->execute();
# See if the row was created and echo success
echo ($query->affected_rows > 0)? 'Success!' : 'Failed';

You should be using password_hash() (storing) and password_verify() (validating) or a bcrypt equivalent library (if your version of php doesn't have those native functions). 您应该使用password_hash() (存储)和password_verify() (验证)或bcrypt等效库(如果您的php版本没有这些本机函数)。 When using these functions, make sure your password column has like 255 character length so not as to cut off the password hash. 使用这些功能时,请确保您的password列的长度为255个字符,以免切断密码哈希。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM