为什么不将php脚本插入数据库表?不返回任何错误

时间:2019-02-15 01:19:56

标签: php mysql

该脚本似乎很有意义,并且没有返回任何错误,但是我网站上的表单中的数据没有插入到数据库表中,我也不知道为什么。目前正在WAMP上对此进行测试。数据库和表名正确,密码也正确。

我尝试过更改代码,使其只进行简单的插入而没有哈希函数。例如,通过为每个帖子分配一个变量名称,然后在插入查询中使用该名称。即使那样也不行。

<?php
// Change this to your connection info.
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = 'PASSWORD';
$DB_NAME = 'DB NAME';
// Try and connect using the info above.
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if ($mysqli->connect_errno) {
    // If there is an error with the connection, stop the script and display the error.
    die ('Failed to connect to MySQL: ' . $mysqli->connect_errno);
}
// Now we check if the data was submitted, isset will check if the data exists.
if (!isset($_POST['username'], $_POST['password'], $_POST['email'])) {
    // Could not get the data that should have been sent.
    die ('Please complete the registration form!<br><a href="register.php">Back</a>');
}
// Make sure the submitted registration values are not empty.
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) {
    // One or more values are empty...
    die ('Please complete the registration form!<br><a href="register.php">Back</a>');
}
// We need to check if the account with that username exists
if ($stmt = $mysqli->prepare('SELECT id, password FROM user WHERE username = ?')) {
    // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute(); 
    $stmt->store_result(); 
    // Store the result so we can check if the account exists in the database.
    if ($stmt->num_rows > 0) {
        // Username already exists
        echo 'Username exists, please choose another!<br><a href="register.php">Back</a>';
    } else {
        // Username doesnt exists, insert new account
        if ($stmt = $mysqli->prepare('INSERT INTO user (username, password, email) VALUES (?, ?, ?)')) {
            // We do not want to expose passwords in our database, so hash the password and use password_verify when a user logs in.
            $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
            $stmt->bind_param('sss', $_POST['username'], $password, $_POST['email']);
            $stmt->execute();
            echo 'You have successfully registered, you can now login!<br><a href="login.php">Login</a>';
        } else {
            echo 'Could not prepare statement!';
        }
    }
    $stmt->close();
} else {
    echo 'Could not prepare statement!';
}
$mysqli->close();
?>

该脚本回显“您已成功注册,现在可以登录!
登录”,但是当我检查数据库表时,什么都没有插入...

0 个答案:

没有答案