PHP数据库插入不起作用

时间:2012-03-11 23:50:13

标签: php

嘿伙计们我有一个问题基本上我在php中创建了一个连接到数据库的脚本。但是,当我尝试将数据发送到数据库时,即使它表示感谢您注册,它也不会进入数据库。但是当我添加信息并检查它是否在数据库中不是。有人可以解决我的问题谢谢:)

<?php
$submitcheck = @$_POST['submit'];
$fullnameCheck = strtolower(strip_tags(@$_POST['fullname']));
$LastNameCheck = strtolower(strip_tags(@$_POST['Lastname']));
$UserNameCheck = strip_tags(@$_POST['username']);
$PasswordCheck = strip_tags(@$_POST['password']);
$ReCheckPass = strip_tags(@$_POST['repassword']);
$date = date("Y-m-d");

if ($submitcheck) {

    // connect to the database
    // checking data 

    if ($PasswordCheck && $ReCheckPass && $UserNameCheck && $fullnameCheck && $LastNameCheck) {


        if ($PasswordCheck == $ReCheckPass) {


            //Check length of username and password

            if (strlen($UserNameCheck) > 25) {

                echo "Username must be less than 25 characters";
            } else {

                if (strlen($LastNameCheck) > 25) {

                    echo "Last name must be less thank 25 charcters";
                } else {

                    if (strlen($fullnameCheck) > 25) {

                        echo "Your Fisrt name is too long must be less than 25";
                    } else {

                        if (strlen($PasswordCheck) > 25 || strlen($PasswordCheck) < 6) {


                            echo "Password must be between 6 to 25";
                        } else {

                            //register the user 
                            $PasswordCheck = md5($PasswordCheck);
                            $ReCheckPass = md5($ReCheckPass);

                            $connect = mysql_connect("localhost", "root", "halo123");
                            mysql_select_db('phplogin');

                            $namecheckers = mysql_query("SELECT * FROM users WHERE username ='$UserNameCheck'");
                            $counts = mysql_num_rows($namecheckers);

                            if ($counts != 0) {

                                die("username in use sorry");
                            }

                            $queryreg = mysql_query("INSERT INTO users()id, username, password, FisrtName, LastName, date) VALUES ('id','$UserNameCheck','$PasswordCheck','$fullnameCheck','$LastNameCheck','$date')");

                            die("Congrats you have been registered <a href='test.php'>Click here</a> to return to login page");
                        }
                    }
                }
            }
        }
        else
            echo "your passwords do not macth";
    }
    else
        echo "Please fill in all the information thank you ";
}
?>


<html


    <head>
    <p>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
        <title>Register</title>




    </head>

<body>

    <form action='register.php' method="POST" >

        Fisrt Name: <input type="text" name="fullname" value="<?php echo $fullnameCheck; ?>"/><br />

        Last Name: <input type="text" name="Lastname" value="<?php echo $LastNameCheck; ?>"/><br />

        Chose Username: <input type="text" name="username" value="<?php echo $UserNameCheck; ?>"/><br />

        Chose Password: <input type="password" name="password" /><br />

        repeat Password: <input type="password" name="repassword" /><br />
        <input type="submit" name ="submit" value="register"/>

    </form>



</body>


</html>

2 个答案:

答案 0 :(得分:3)

INSERT INTO users()id, username, password, FisrtName, LastName, date) VALUES ('id','$UserNameCheck','$PasswordCheck','$fullnameCheck','$LastNameCheck','$date'‌​)

当然应该是:

INSERT INTO users(id, username, password, FisrtName, LastName, date) VALUES ('id','$UserNameCheck','$PasswordCheck','$fullnameCheck','$LastNameCheck','$date'‌​)

答案 1 :(得分:1)

您需要检查mysql_query的结果,以查看查询是否确实有效。 您拥有的代码现在假定它可以工作并忽略可能发生的任何错误。

如果mysql_query返回FALSE,则可以使用mysql_error()

检查错误