首页 > 解决方案 > 无法将注册页面中的数据放入 Mysql 数据库(000webhost)

问题描述

我已经开始使用 000webhost 使我的网站在线。

因为 000webhost 有一个专用的 Mysql 用于他们的数据库,所以我从那里创建了一个与我拥有的相同的 Mysql(在 Xampp 中)。

问题是我做的注册 PHP 不再工作了。

这是我用于注册的 PHP。

<?php

session_start();

require "db_conn.php";
require_once "emailControl.php";

$error = array();
$username = "";
$email = "";

if (isset($_POST['signup-btn'])) {
    $username = $_POST['username'];

    $email = $_POST['email'];

    $password = $_POST['password'];

    $passwordConf = $_POST['passwordConf'];

    if (empty($username)) {
        $error['username'] = "Username required";
    }

    if (empty($email)) {
        $error['email'] = "Email required";
    }
    if (empty($password)) {
        $error['password'] = "Password required";
    }

    if ($password !== $passwordConf) {
        $error['password'] = "The passwords does not match";
    }

    $emailQuery = "SELECT * FROM users WHERE email=? LIMIT 1";
    $stmt = $conn->prepare($emailQuery);
    $stmt->bind_param('s', $email);
    $stmt->execute();
    $result = $stmt->get_result();
    $userCount = $result->num_rows;
    $stmt->close();

    if ($userCount > 0) {
        $error['email'] = "Email already exists";
    }

    $usernameQuery = "SELECT * FROM users WHERE user_name=? LIMIT 1";
    $stmt = $conn->prepare($usernameQuery);
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $result = $stmt->get_result();
    $userCount = $result->num_rows;
    $stmt->close();

    if ($userCount > 0) {
        $error['username'] = "Username already exists";
    }

    if (count($error) === 0) {
        $password = password_hash($password, PASSWORD_DEFAULT);
        $token = bin2hex(random_bytes(50));

        $verified = false;

        $sql = "INSERT INTO users (user_name, email, verified, token, password) VALUES (?, ?, ?, ?, ?)";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param('ssbss', $username, $email, $verified, $token, $password);

        if ($stmt->execute()) {
            $user_id = $conn->insert_id;
            $_SESSION['id'] = $user_id;
            $_SESSION['username'] = $username;
            $_SESSION['email'] = $email;
            $_SESSION['verified'] = $verified;

            $_SESSION['message'] = "you are now logged in!";
            $_SESSION['validate'] = "An email will be send to verify your account! please check your email and click the link inside.";
            $_SESSION['validate1'] = "You are now verified! you can now enter and enroll for SHS in Theresian School of Cavite! Thank you and have a great day!";

            header('location: index.php');
            exit();
        } else {
            $error['db_error'] = "Database error: Failed to register";
        }
    }
}

我已经推断出注册网页中表单中的数据没有通过数据库,而只是显示 else 语句。

我不明白为什么这不起作用,因为当我尝试对我的计算机上的 Mysql 执行此操作时,它运行良好。

有谁知道为什么会这样?

标签: phpmysqli

解决方案


推荐阅读