PHP SQL为什么这会给我一个没有细节的错误

时间:2018-03-14 19:19:29

标签: php mysql sql database

您好我试图在我的数据库中插入数据。但我继续得到同样的错误,例如:

Error: INSERT INTO users (username, password) VALUES ('fff', '$2y$10$YUd1AErIj4RGRnjkFkYlkOn.s9OV62sq8.HVGO2jeE8dSthpgp6ey');

没有任何令人沮丧的细节。我是PHP和SQL的新手,所以它不是有史以来最好的编写代码,我知道我应该使用预备语句。

<?php
require_once '../connection/connection.php';
/**
 * Created by PhpStorm.
 * User: ezrab
 * Date: 3/14/2018
 * Time: 5:40 PM
 */
$username = $_POST['username'];
$password = $_POST['password'];
//var_dump($hashed_password);
if (isset($_POST['submit'])) {
    if (!empty($username) || !empty($password)) {
        if (preg_match('/^[A-Za-z]?[A-Za-z ]*$/', $username) || preg_match('/^[A-Za-z]?[A-Za-z ]*$/', $password)) {
            $hashPwd = password_hash($password, PASSWORD_DEFAULT);

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

            if ($conn->query($sql) === TRUE) {
                echo "Worked!";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        } else {
            echo "You can't use certain characters.";
        }
    } else {
        echo "You have to fill in all fields.";
    }
} else {
    echo "THOU SHALL NOT PASS!";
}
$conn->close();

编辑:添加了我的connection.php文件以获取更多信息。

<?php
$servername = "-----";
$username = "-----";
$password = "------";
$dbname = "------";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$conn->close();

1 个答案:

答案 0 :(得分:0)

编辑:

$conn->close();

out of connection.php和问题应该解决

您正在打开,然后在进行查询之前立即关闭连接