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

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

报告相同问题?

悬赏问题

  • ¥15 EtherCAT的问题,创建一个XML文件
  • ¥15 微信公众号羽毛球自动抢定场程序制作
  • ¥15 Fluent UDF 编写
  • ¥15 xcode开发的macos应用,如何使窗口居中显示?
  • ¥20 统信uos升级apt后启动失败
  • ¥15 求指导储层饱和度及含水率测井解释
  • ¥200 建三维地震工区写入sgy到指定目录
  • ¥30 靶向捕获测序探针设计自学
  • ¥15 写代码写代码单片机代码写代码写数字按键代码
  • ¥15 django按照距离进行排序