数据不会插入MySQL数据库以便在登录/注册表单中注册

时间:2016-06-05 17:41:12

标签: php mysql forms login registration

当我尝试注册新帐户时,我特意收到了自定义错误 - “由于技术错误,我们目前无法创建新帐户。”

如果没有将信息发布到我的MySQL数据库,我会将此错误设置为回显。我不知道为什么没有发布

所以错误在于注册部分(或会话部分),但我在这里发布了所有相关代码。

这都包含在一个正常运行的html中。

这是非常开始的PHP代码(上面是!DOCTYPE html)启动我的会话。

<?php
session_start();
if(isset($_SESSION['sess_user'])){

header("Location: profile.php");
exit();
}
?> 

这是登录表单的代码:     

            <ul>
                <li>
<input type="text" name="username" id="uname_log"         value="" placeholder="Username" class="required requiredField Email fg-input text fg-fw" />
                </li>
                <li>
<input type="password" name="password" id="password_log" value="" placeholder="Password" class="required requiredField  fg-input text fg-fw" />
                </li>
                <li>
               <div class="chkbox"> <input type="checkbox" id="remember" name="remember" value="true"/> Remember me</div>
                </li>
                <li>
                    <div class="clear"></div>                       
                    <input class="submit signin_btn fg-btn blue large inline fg-fw bold" type="submit" name="submit2" value="Sign In"/>
                    <input type="hidden" name="submitted" id="submitted" value="" />
                </li>
            </ul>
        </form> 
<?php

        if (isset($_POST["submit2"])) {
            $username =$_POST['username'];
            $password =$_POST['password'];

            $connect=mysqli_connect('localhost', 'root', 'U7wQkQ**') or die(mysqli_error());
            mysqli_select_db($connect, 'test6') or die("Cannot select database");

            $query=mysqli_query($connect, "SELECT * FROM test WHERE username ='".$username."' AND password='".$password."'");
            $numrows=mysqli_num_rows($query);

            if ($numrows!=0){
                while ($row=mysqli_fetch_assoc($query)) {
                    $dbusername=$row['username'];
                    $dbpassword=$row['password'];
                }
            if ($username == $dbusername && $password == $dbpassword){



            $_SESSION['sess_user'] = $username;



            }
            } else {
                echo "Invalid username or password";
            }
        }
        ?>

这是注册表格的代码:

<form action="" id="freesigninForm" class="freesigninForm" method="post">
                 <ul>

                <li>
                    <input type="text" name="username" id="reg_email" value="" placeholder="Username" class="required requiredField Email fg-input text fg-fw" />
                </li>
                <li>
                    <input type="password" name="password" id="reg_password" value="" placeholder="Password" class="required requiredField Password fg-input text fg-fw" />
                </li>
                <li>
                    <input type="password" name="password2" id="reg_password2" onkeyup="checkPasswordMatch();" value="" placeholder="Repeat Password" class="required requiredField Password fg-input text fg-fw" />
                </li>

                <div class="clear"></div>
                <li>
                    <input style="display: none;" id="reg_check3" type="checkbox" name="check" value="YES" checked/>  
                    <span style="display: none;"><a href='http://www.formget.com/terms-conditions/' target='_blank'>I agree with terms and conditions</a></span>
                    <button id="buttonActivate" class="submit fg-btn blue large inline fg-fw bold" type="submit" name="submit" >Create Account</button>

                </li>
            </ul>
        </form>
<?php

if (isset($_POST["submit"])) {
$username = $_POST['username'];  
$password = $_POST['password'];

$connect=mysqli_connect('localhost', 'root', 'U7wQkQ**') or die(mysqli_error());
mysqli_select_db($connect, 'test6') or die("Cannot select database");

$query=mysqli_query($connect, "SELECT * FROM test WHERE username='".$username."'");
$numrows = mysqli_num_rows($query);

if($numrows== 0){

    $sql ="INSERT INTO test (username, password) VALUES ('$username', '$password')";


    $result=mysqli_query($connect, $sql);

    if ($result) {
        echo "Account successfully created";

    } else { 
        echo "We are unable to create a new account at this time due to technical errors.";
    }

    } else {
        echo "Username already exists. Please try again with a unique username!";

    }
}

?>

1 个答案:

答案 0 :(得分:0)

我在chris85的帮助下想出来了。

基本上我曾经使用echo mysqli_error($ connect)来解决mysqli错误;在我的代码中,我得到一个错误,说重复条目&#39; 0&#39;关键&#39; PRIMARY&#39;。

所以基本上我的数据库每次都试图覆盖主键数据(我没有提到它,但是这段代码昨天起作用但今天没有用,我也不明白为什么)

将主键(ID)设置为A_I或自动递增后,表格正在更新:D