首页 > 解决方案 > “成功”注册后数据未显示在数据库中

问题描述

嘿,所以我遵循了一个关于如何使用 php 创建功能注册系统的教程,所有代码似乎都可以正常工作,但是我在注册表中输入的数据并没有显示在我的数据库中,即使脚本给出了我已成功注册的输出。有谁知道解决这个问题?

<?php
// Connect to the db
$DATABASE_HOST = 'localhost';
$DATABSE_USER = 'root';
$DATABSE_PASS = '';
$DATABSE_NAME = 'phplogin';
// Try to connect
$con = mysqli_connect($DATABASE_HOST, $DATABSE_USER, $DATABSE_PASS, $DATABSE_NAME);
if(mysqli_connect_errno()) {
    //If there is an error stop the script and display the error
    exit('Failed to connect to MySQL: '. mysqli_connect_error());
}
//check if the data already exists
if (!isset($_POST['username'], $_POST['password'], $_POST['email'])) {
    //Could not get the data that should have been sent
    exit('Please register first');
}
//Submitted registration values are not empty
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) {
    //if empty exit the script
    exit('Please complete the register form');
}
//check if the username has been used already
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
    //encrypt password
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute();
    $stmt->store_result();

//store the results to be able to check the db
if ($stmt->num_rows > 0) {
    //username already exists
    echo 'Username already used';
} else {
    //Insert new account
    if ($stmt = $con->prepare('INSERT INTO accounts (username, password, email) VALUES (?, ?, ?)')) {
        //hash the password and use password_verify
        $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
        $stmt->bind_param('sss', $_POST['username'], $password, $POST['email']);
        $stmt->execute();
        echo 'You have succesfully registered, you can now login';
    }}
    $stmt->close();
} else {
    //Something wrong with the sql statement
    echo 'Could not prepare Statement!';
}

$con->close();
?>

标签: phpmysqliregistration

解决方案


在开发时将错误检查添加到代码中是非常好的做法。一个简单的方法是在 php 代码标签内的 php 页面顶部添加它,以至少在页面上回显错误 -

error_reporting(E_ALL);
ini_set('display_errors', 1);

您的代码应如下所示,应返回一条记录。如果您有错误报告,它会告诉您错误在哪里 -

// Connect to the db
$DATABASE_HOST = 'localhost';
$DATABSE_USER = 'root';
$DATABSE_PASS = '';
$DATABSE_NAME = 'phplogin';
// Try to connect
$con = mysqli_connect($DATABASE_HOST, $DATABSE_USER, $DATABSE_PASS, $DATABSE_NAME);

if(mysqli_connect_errno()) {
    //If there is an error stop the script and display the error
    exit('Failed to connect to MySQL: '. mysqli_connect_error());
}

//check if the data already exists
if (!isset($_POST['username']) || !isset(['password']) || !isset($_POST['email'])) {
    //Could not get the data that should have been sent
    exit('Please register first');
} else {
    //check if the username has been used already
    if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
        //encrypt password
        $stmt->bind_param('s', $_POST['username']);
        $stmt->execute();
        $stmt->store_result();

        //username already exists
        echo 'Username already used';
    } else {
        //Insert new account
        if ($stmt = $con->prepare('INSERT INTO accounts (username, password, email) VALUES (?, ?, ?)')) {
            //hash the password and use password_verify
            $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
            $stmt->bind_param('sss', $_POST['username'], $password, $POST['email']);
            $stmt->execute();
            echo 'You have succesfully registered, you can now login';
        } else {
            echo 'Data not inserted...';
        }
    }
    $stmt->close();
}

$con->close();

推荐阅读