数据不会插入,但不会显示错误

时间:2016-04-03 19:33:40

标签: php mysql database web

我正在尝试使用xampp server创建一个注册页面。我写了一个PHP代码,但数据没有插入数据库,甚至没有显示错误。我可以找出问题所在我检查了我的数据库是否已连接。它已连接,因为我试图通过输入错误的代码重新检查,然后屏幕上弹出错误信息。我的代码如下:

在这里输入代码

帮我解决这个问题。实际上我无法解决这个问题。我试图使用xampp server创建一个注册页面。我写了一个PHP代码,但数据没有插入数据库,甚至没有显示错误。我可以找出问题。我查了一下如果我的数据库已连接或未连接。它已连接,因为我尝试通过键入错误的代码重新检查,然后在屏幕上弹出错误消息。我的代码如下所示

<?php include("connect.php"); ?>

<?php
$reg = @$_POST['reg'];
//declraing variable to prevent error!
$un = "";//username
$em = "";//email
$pswd = "";//password
$pswd2 = "";//password2
$u_check = "";//check if username exists
//registration form
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$pswd = strip_tags(@$_POST['password']);
$pswd2 = strip_tags(@$_POST['password2']);

if ($reg){
    if($em){
        //check if user already exists
        $u_check = mysql_query("SELECT username FROM user WHERE username = '$un'");
        //count the amount of rows where username=$un
        $check = mysql_num_rows($u_check);
        if($check==0){
            //check all of the fields have been filled in
            if($un&&$em&&$pswd&&$pswd2){

                //check that passwords match
                `if($pswd==$pswd2){
                     if(strlen($un)>=30){
                         echo "the maximum limit for username is 30 characters!";
                     }
                     else
                    {
                        if(strlen($pswd)>30||strlen($pswd2)>30){
                            echo "your password must be 6 to 30 characters long!";
                        }
                        else
                        {
                            //encrypt password and password2 using md5 before sending to database
                            $pswd = md5($pswd);
                            $pswd2 = md5($pswd2);
                            $query = mysql_query("INSERT INTO user VALUES ('','$un','$em','$pswd')");
                            die("<h2>Welcome to Invite!<h2>Login to your account to get started!");
                        }
                    }
                } 
                else{
                    echo "your password do not match!";
                }
            }
            else{
                echo "please fill up all the fields";
            }
        }
        else
        {
            echo "username already taken!";
        }
    }
}
?>

<!DOCTYPE html>
<html >

  <head>
    <meta charset="UTF-8">
    <title>register page</title>
    <link rel="shortcut icon" href="themes/assets/ico/icon.ico">
    <link rel="stylesheet" href="css/sign.css">
  </head>

  <body>

    <img src="back.jpg" height="619px" width="1366px">

    <div class="wrapper">

        <div class="container">

            <h1>Sign Up Form</h1>

            <form class="form">
                <input type="text" name="username" placeholder="Enter Name">
                <input type="text" name="email" placeholder="Enter Email">xxxxx@xxx.com
                <input type="password" name="password" placeholder="Enter Password">password must contain atleast six characters

                <input type="password" name="password2 " placeholder="Re-type Password">confirm your password<br>

                <button type="submit" name="reg" id="register-button">Sign Up!
                </button>

            </form>

        </div>

        <ul class="bg-bubbles">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>

    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

    <script src="js/index.js"></script>

  </body>
</html>

2 个答案:

答案 0 :(得分:0)

首先,你应该在php文件的开头打开error_reporting,参见http://php.net/manual/en/function.error-reporting.php

<?php
error_reporting(-1);
$reg = @$_POST['reg'];

第二,你必须手动检查MySQL错误,它不会自动显示它们:

$query = mysql_query("INSERT INTO user VALUES ('','$un','$em','$pswd')");
if(!query) {
    echo mysql_error();
}

我还想指出你在if

的开头有一个反击
//check that passwords match
            `if($pswd==$pswd2){
                 if(strlen($un)>=30){

也像罗伯特所说,你应该切换到mysqli,不推荐使用mysql方法,不应该再使用它了。

答案 1 :(得分:0)

    //check that passwords match
  remove this -------->`if($pswd==$pswd2){
                         if(strlen($un)>=30){
                             echo "the maximum limit for username is 30 characters!";
                         }