PHP 在刷新时不断插入

标签 php mysql sql forms mysqli

我目前正在为我的应用程序注册,直到我遇到这个问题,每当我进入我的页面时,它都会在我的 users 表中创建一个新行。似乎当我重新加载页面时,表单会自动提交。我曾提到this问题:

Use header("location: "); after you are done inserting, that way a fresh form will be displayed and refreshing should not lead to submitting again.

但是,当我这样做时发生的只是一个大的重定向循环,阻止我一起访问该页面。

这是我的注册代码:

<?php 

    global $conn;
    global $register;
    global $error;

    $username = $_POST['username'];
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $password = $_POST['password'];
    $confirmPassword = $_POST['confirmPassword'];

    define(HEADER_TITLE, "Register");
    include_once 'template/header.php';

    if (isset($_POST['register'])) {
        $register->register($username, $firstName, $lastName, $email, $phone, $password, $confirmPassword);
    }

?>
<div class="container">
    <div id="hoogley_login">
        <h2>Register</h2><hr>
            <div class="alert alert-danger" role="alert">
            <?php
            if ($error->display() != null ) {
                #echo '<div class="alert alert-danger" role="alert">';
                $error->display();
            } else {
                #echo '<div>';
            }
        ?>
        </div>
        <form action="register" method="post">
            <input type="text" class="form-control" name="firstName" placeholder="First Name" value="<?php echo $_GET['firstName']; ?>">
            <br>
            <input type="text" class="form-control" name="lastName" placeholder="Last Name" value="<?php echo $_GET['lastName']; ?>">
            <br>
            <input type="text" class="form-control" name="username" placeholder="Username" value="<?php echo $_GET['username']; ?>">
            <br>
            <input type="email" class="form-control" name="email" placeholder="E-Mail Address" value="<?php echo $_GET['email']; ?>">
            <br>
            <input type="tel" class="form-control" name="phone" placeholder="Phone Number" value="<?php echo $_GET['phone']; ?>">
            <br>
            <input type="password" class="form-control" name="password" placeholder="Password">
            <br>
            <input type="password" class="form-control" name="confirmPassword" placeholder="Confirm Password">
            <br>
            <input type="submit" class="btn btn-info btn-lg" name="register" value="Register!">
            <br><br>
            <a href="/login">Have an account? Login!</a>
        </form>
    </div>
</div>



<?php include_once 'template/footer.php'; ?>

我的register.class.php:

<?php


/**
* User Registration
*/
class register {

public function register($reg_username = '', $reg_firstName = '', $reg_lastName = '', $reg_email = '', $reg_phone = '', $reg_password = '', $reg_confirmPassword = '') {

    global $conn;
    global $error;

    // TO PREVENT AN UNDEFINED INDEX
    // First Name
    if(isset($reg_firstName)) {
        $reg_firstName = stripslashes(htmlspecialchars($conn->real_escape_string($reg_firstName)));
    } else {
        $error->set('312');
    }

    // Last Name
    if(isset($reg_lastName)) {
        $reg_lastName = stripslashes(htmlspecialchars($conn->real_escape_string($reg_lastName)));
    } else {
        $error->set('313');
    }

    // Username
    if(isset($reg_username)) {
        $reg_username = stripslashes(preg_replace('/\s+/', '', htmlspecialchars($conn->real_escape_string($reg_username))));
    } else {
        $error->set('311');
    }

    // Email
    if(isset($reg_email)) {
        $reg_email = stripslashes(preg_replace('/\s+/', '', htmlspecialchars($conn->real_escape_string($reg_email))));
    } else {
        $error->set('314');
    }

    // Phone
    if(isset($reg_phone)) {
        $reg_phone = stripslashes(preg_replace("/[^0-9]/", "", htmlspecialchars($conn->real_escape_string($reg_phone))));
    } else {
        $error->set('315');
    }

    // Password
    if(isset($reg_password)) {
        $reg_password = stripslashes(htmlspecialchars($conn->real_escape_string($reg_password)));
    } else {
        $error->set('316');
    }

    // Password
    if(isset($reg_confirmPassword)) {
        $reg_password = stripslashes(htmlspecialchars($conn->real_escape_string($reg_confirmPassword)));
    } else {
        $error->set('317');
    }

    // Check if username exists in the database
    $stmt = $conn->prepare("SELECT * FROM users WHERE username=?");
    $stmt->bind_param('s', $reg_username);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows == 0) {

        $stmt = $conn->prepare("SELECT * FROM users WHERE email=?");
        $stmt->bind_param('s', $reg_email);
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->num_rows == 0) {

            $stmt = $conn->prepare("SELECT * FROM users WHERE phone=?");
            $stmt->bind_param('s', $reg_phone);
            $stmt->execute();
            $stmt->store_result();

            if ($stmt->num_rows == 0) {

                // Do passwords match?
                if ($reg_password == $reg_confirmPassword) {

                    $reg_passwordEncrypt = hash("sha256", $reg_password);

                    // Insert data
                    $stmt = $conn->prepare("INSERT INTO users (username, firstName, lastName, email, phone, password, company, website, bio) VALUES (?, ?, ?, ?, ?, ?, '', '', '')");
                    $stmt->bind_param("ssssss", $reg_username, $reg_firstName, $reg_lastName, $reg_email, $reg_phone, $reg_passwordEncrypt);
                    $stmt->execute();
                    header("Location: /login");

                } else {
                    // Set Error
                    $error->set('308');
                }

            } else {
                // Set Error
                $error->set('306');
            }

        } else {
            // Set Error
            $error->set('304');
        }
    } else {
        // Set Error
        $error->set('301');

    }


}

}

$register = new register;

谢谢!

最佳答案

这是因为你的方法名称与类名称相同。它作为 PHP 4 构造函数工作,并在初始化对象时调用该方法。

https://wiki.php.net/rfc/remove_php4_constructors

关于PHP 在刷新时不断插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45446396/

相关文章:

sql - MS SQL Server 最后插入的 ID

php - DOMPDF 0.5.1 - 新的服务器环境破坏了 PDF 输出格式

mysql - 在重复键更新中使用不同列插入选择

sql - 如何在 Spark DataFrame 上应用自定义过滤功能

mysql - 获取引用表的列总数

sql - 检索最后(最新)不同的最高值

sql - 删除数据库后,Azure SQL 尝试创建自动 SQL 导出

php - 如何制作 Firefox,不缓存我的网站

php - ZipArchive,我需要创建新文件并覆盖现有

php - brew install homebrew/php/php56 给我一个错误