douguo6472 2018-03-14 19:19
浏览 38
已采纳

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

Hi I'm trying to insert data in my database. But I keep on getting the same error for example:

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

without any details which is very frustrating. I'm new to PHP and SQL so it's not the best written code ever and I know I should use prepared statements.

<?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();

EDIT: Added my connection.php file for more information.

<?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条回答 默认 最新

  • dongxi5494 2018-03-14 19:24
    关注

    EDIT:

    Take

    $conn->close();
    

    out of connection.php and problem should be solved

    You were opening and then immediately closing the connection before a query could be made

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Yolov5训练报错
  • ¥15 Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面
  • ¥15 有没有人知道这种提示怎么关?现在不做ts项目了不知道咋关了,求解,现在我以前的js项目都是这种提示了
  • ¥15 为什么mysql做了碎片化处理data_free还是很高
  • ¥15 single positional indexer is out-of-bounds
  • ¥15 LSTM 模型数据量需要多少?
  • ¥15 mysql数据抓包开发报表
  • ¥15 linux系统下安装office打不开
  • ¥20 rsync脚本分发错误,文件不齐全
  • ¥15 labview顺序结构与时间延时