简体   繁体   English

PHP + MYSQL不会插入数据库

[英]PHP+MYSQL Won't Insert Into Database

Ok on my register page it just refreshes. 好的,在我的注册页面上,它只是刷新。 My login lets you log in but my register just reloads the page. 我的登录名可以让您登录,但我的注册只是重新加载页面。

Also I'm trying add a transaction code on it when you register 另外,我正在尝试在您注册时在上面添加交易代码

This is my register page code 这是我的注册页面代码

<?php
error_reporting(0);
ob_start();
session_start();

// Include DB
require('inc/Database.php');
$db = new Database();
$pageName = "Register";

// Include user class
include('inc/user.php');
$user = new User($db);

if(isset($_POST['submit'])){

            $_POST['username'] = preg_replace('/[^A-Za-z0-9\-]/', '', $_POST['username']);
            $username = $_POST['username'];
            // $password = $_POST['password'];
            $password = hash("sha256", $_POST['password']);
            $count = $db->count('SELECT COUNT(username) FROM members WHERE username = ?', array($username));

            if (!$count) {
                $activation = md5(uniqid(rand(),true));
                $result = $db->insert('INSERT INTO members (username,transaction,ownerIP,signupDate,password,email,active) VALUES (?, ?, NOW(), ?, ?, ?, ?)', array($username, $transaction, $ownerIP, $password, $_POST['email'], $activation));
                if ($result) {
                    $to = $_POST['email'];
                    $subject = "Welcome to ShareTracks!";
                    $body = "Thank you for registering at ShareTracks! The number one music app and community!\n\nActivate your account by simply clicking on the link below:\n\n http://sharetracks.net/activate.php?user=$username&code=$activation";
                    $additionalheaders = "From: accounts@sharetracks.net\r\n";
                    $additionalheaders .= "Reply-To: accounts@sharetracks.net";
                    mail($to, $subject, $body, $additionalheaders);
                    header('Location: login.php?action=joined');
                } else {
                    header('Location register.php?error=data');
                }
            } else {
                header('Location: register.php?error=taken');
            }
}
?>

This is my register html form: 这是我的注册html表格:

        <div class="account-form">
            <form class="form-signup" role="form" method="post" action="" autocomplete="off">
                <h3><strong>Create</strong> your account</h3>
          <?php if($_GET['error'] == 'data') {
            echo '<center><div class="alert alert-info"><i class="fa fa-warning"></i> It seems you have entered invalid data, please sign up again using valid characters [A-Z/1-9].</div></center>'; 
          } ?>

          <?php if($_GET['error'] == 'taken') {
            echo '<center><div class="alert alert-info"><i class="fa fa-warning"></i> The username you have chosen is already taken. Try another one.</div></center>'; 
          } ?>                  
                <div class="append-icon">
                    <input type="text" name="username" id="username" placeholder="Username" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" class="form-control form-white username" required>
                    <i class="icon-user"></i>
                </div>
                <div class="append-icon">
                    <input type="email" name="email" id="email" class="form-control form-white email" placeholder="Email Address" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" required>
                    <i class="icon-envelope"></i>
                </div>
                <div class="append-icon m-b-10">
                    <input type="password" name="password" id="password" class="form-control form-white password" placeholder="Password" required>
                    <i class="icon-lock"></i>
                </div>
                <div class="append-icon m-b-20">
                    <input type="password" name="passwordConfirm" id="passwordConfirm" class="form-control form-white password2" placeholder="Confirm Password" required>
                    <i class="icon-lock"></i>
                </div>
                <div class="append-icon m-b-20">
                    <input type="text" name="transaction" id="transaction" class="form-control form-white transaction" placeholder="PayPal Transaction" required>
                    <i class="fa fa-usd"></i>
                </div>                  
                <div class="terms option-group">
                    <label  for="terms" class="m-t-10">
                    <input type="checkbox" name="terms" id="terms" data-checkbox="icheckbox_square-blue" required/>
                    I agree with terms and conditions
                    </label>  
                </div>
                <div class="m-t-20">
                    <button name="submit" type="submit" class="btn btn-lg btn-dark btn-rounded" data-style="expand-left">Register</button>
                </div>
            </form>
            <div class="form-footer">
                <div class="clearfix">
                    <p class="new-here"><a href="login.php">Already have an account? Sign In</a></p>
                </div>
            </div>
        </div>
    </div>

When I click the submit button it just refreshes the page. 当我单击提交按钮时,它只会刷新页面。

You need to set your form action="" to your register page. 您需要将表单action =“”设置到注册页面。 Your current blank action makes it submit to the page that your form is on, not where your registration code is. 您当前的空白操作使其提交到表单所在的页面,而不是您的注册代码所在的页面。 So it just refreshes the current page. 因此,它只是刷新当前页面。

Set action="" with the file name of your register page code 使用注册页面代码的文件名设置action =“”

example : 例如:

if you have two different files one for form submission and the other one for register page code with file name "abc.php" 如果您有两个不同的文件,一个用于表单提交,另一个用于注册页面代码,文件名为“ abc.php”

in your form submission action="abc.php" 在您的表单Submit action =“ abc.php”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM