为什么我的网站不将用户输入数据存储在我的数据库中?

时间:2014-08-07 19:37:13

标签: php html mysql post mysqli

您好我是编程初学者,我正在尝试在我的网站上创建一个简单的注册函数,一个用户填写的简单表单,然后它应该将数据存储在我的数据库中,问题是一切正常(我想):)但数据/用户信息不存储。

   <?php
include ("./inc/header.inc.php") $reg = @$_POST['reg'];

$fn = ""; //first name
$ln = ""; //Last name
$un = ""; //User name
$em = ""; //Email
$em2 = ""; //Email2
$pswd = ""; //Password
$pswd2 = ""; //Password 2
$d = ""; //sign up date

// registrering av nya användare
// ucheck kontrollerar om användaren redan finns

$fn = strip_tags(@$_POST['first_name']);
$ln = strip_tags(@$_POST['last_name']);
$un = strip_tags(@$_POST['Username']);
$em = strip_tags(@$_POST['Email']);
$em2 = strip_tags(@$_POST['Email2']);
$pswd = strip_tags(@$_POST['Password']);
$pswd2 = strip_tags(@$_POST['Password2']);
$d = date("Y-n-d");

if ($reg)
    {
    if ($em == $em2)
        {
        $u_check = mysqli_query($mysqli, "SELECT Username FROM users WHERE Username='$un'");
        $check = mysqli_num_rows($u_check);
        if ($check == 0)
            {
            if ($fn && $ln && $un && $em && $em2 && $pswd && $pswd2)
                {
                if ($pswd == $pswd2)
                    {
                    if (strlen($un) > 25 || strlen($fn) > 25 || strlen($ln) > 25)
                        {
                        echo "The maximum limit for Username/last name and first name is 25 characters :(";
                        }
                      else
                        {
                        if (strlen($pswd) > 30 || strlen($pswd) < 5)
                            {
                            echo " Your password must be more than 5 characters or less then 30 characters :(";
                            }
                          else
                            {

                            // kryptering av password 1 och 2 med hjälp av md5 innan det sätts i databasen

                            $pswd = md5($pswd);
                            $pswd2 = md5($pswd2);
                            $query = mysqli_query($mysqli, "INSERT INTO users VALUES (``,`$un`,`$fn`,`$ln`,`$em`,`$pswd`,`$d`,`0`)");
                            die("<h2>You are now a member of the village</h2> Login to get started...");
                            }
                        }
                    }
                  else
                    {
                    echo "Your passwords don´t match :(";
                    }
                }
              else
                {
                echo "Please fill in all of the fields";
                }
            }
          else
            {
            echo "Username already taken ...";
            }
        }
      else
        {
        echo "Your emails dont match :( ";
        }
    }

?>
                    <div style="width: 1000px; margin:0px auto 0px auto;">
                    <table>
                        <tr>
                            <td width="60%" valign="top">
                                <h2>Join Today!<h2>
                                </td>
                                <td width="40%" valign="top">
                                <h2>Enter Your information and sign up today!<h2>
                                <form action="#" method= "POST">
                                    <input type="text" name="first_name" Size:"30" placeholder="Firstname"/><br/><br/>
                                    <input type="text" name="last_name" Size:"30" placeholder="Lastname"/><br/><br/>
                                    <input type="text" name="Username" Size:"30" placeholder="Username"/><br/><br/>
                                    <input type="text" name="Password" Size:"30" placeholder="Password"/><br/><br/>
                                    <input type="text" name="Password2" Size:"30" placeholder="Password Confirmation"/><br/><br/>
                                    <input type="text" name="Email" Size:"30" placeholder="Email"/><br/><br/>
                                    <input type="text" name="Email2" Size:"30" placeholder="Email Confirmation"/><br/><br/>
                                    <input type="Submit" name="reg" Size:"30" placeholder="Sign-up!">
                                    </form>
                                    </td>
                                    </tr>


    <?php include ( "./inc/footer.inc.php" )?>

1 个答案:

答案 0 :(得分:0)

你必须检查你的mysqli数据库连接。 @是PHP中的错误抑制运算符。

PHP支持一个错误控制操作符:at符号(@)。当在PHP中添加表达式之前,将忽略该表达式可能生成的任何错误消息。

请参阅:

错误控制操作符 @运算符的错误用法 更新:

在您的示例中,它在变量名称之前使用,以避免出现E_NOTICE错误。如果在$ _POST数组中,未设置hn键,则会抛出E_NOTICE消息,但在那里使用@来避免E_NOTICE。

请注意,您也可以将此行放在脚本之上以避免E_NOTICE错误:

error_reporting(E_ALL ^ E_NOTICE); PHP6注意:

因为@运算符非常慢,所以它不适用于ini_set,例如@ini_set。

你应该尽可能避免使用它。

<?php
error_reporting(E_ALL ^ E_NOTICE);
include ("./inc/header.inc.php"); 
$reg = $_POST['reg'];

$fn = ""; //first name
$ln = ""; //Last name
$un = ""; //User name
$em = ""; //Email
$em2 = ""; //Email2
$pswd = ""; //Password
$pswd2 = ""; //Password 2
$d = ""; //sign up date

// registrering av nya användare
// ucheck kontrollerar om användaren redan finns

$fn = mysql_real_escape_string($_POST['first_name']);
$ln = mysql_real_escape_string($_POST['last_name']);
$un = mysql_real_escape_string($_POST['Username']);
$em = mysql_real_escape_string($_POST['Email']);
$em2 = mysql_real_escape_string($_POST['Email2']);
$pswd = mysql_real_escape_string($_POST['Password']);
$pswd2 = mysql_real_escape_string($_POST['Password2']);
$d = date("Y-n-d");

if ($reg)
{
    if ($em == $em2)
        {
            $u_check = mysqli_query($mysqli, "SELECT Username FROM users WHERE Username='$un'");
            $check = mysqli_num_rows($u_check);
            if ($check == 0)
            {
                     if ($fn && $ln && $un && $em && $em2 && $pswd && $pswd2)
                    {
                        if ($pswd == $pswd2)
                            {
                                    if (strlen($un) > 25 || strlen($fn) > 25 || strlen($ln) > 25)
                                        {
                                        echo "The maximum limit for Username/last name and first name is 25 characters :(";
                                        }
                                      else
                                        {
                                                if (strlen($pswd) > 30 || strlen($pswd) < 5)
                                                {
                                                echo " Your password must be more than 5 characters or less then 30 characters :(";
                                                }
                                              else
                                                {
                                                // kryptering av password 1 och 2 med hjälp av md5 innan det sätts i databasen

                                                $pswd = md5($pswd);
                                                $pswd2 = md5($pswd2);
                                                $query = mysqli_query($mysqli, "INSERT INTO users VALUES (``,`$un`,`$fn`,`$ln`,`$em`,`$pswd`,`$d`,`0`)");
                                                die("<h2>You are now a member of the village</h2> Login to get started...");
                                                }
                                        }
                            }
                          else
                            {
                            echo "Your passwords don´t match :(";
                            }
                    }
                      else
                        {
                        echo "Please fill in all of the fields";
                        }
            }
          else
            {
            echo "Username already taken ...";
            }
        }
      else
        {
        echo "Your emails dont match :( ";
        }
}

?>
                    <div style="width: 1000px; margin:0px auto 0px auto;">
                    <table>
                        <tr>
                            <td width="60%" valign="top">
                                <h2>Join Today!<h2>
                                </td>
                                <td width="40%" valign="top">
                                <h2>Enter Your information and sign up today!<h2>
                                <form  method= "POST">
                                    <input type="text" name="first_name" Size:"30" placeholder="Firstname"/><br/><br/>
                                    <input type="text" name="last_name" Size:"30" placeholder="Lastname"/><br/><br/>
                                    <input type="text" name="Username" Size:"30" placeholder="Username"/><br/><br/>
                                    <input type="text" name="Password" Size:"30" placeholder="Password"/><br/><br/>
                                    <input type="text" name="Password2" Size:"30" placeholder="Password Confirmation"/><br/><br/>
                                    <input type="text" name="Email" Size:"30" placeholder="Email"/><br/><br/>
                                    <input type="text" name="Email2" Size:"30" placeholder="Email Confirmation"/><br/><br/>
                                    <input type="Submit" name="reg" Size:"30" placeholder="Sign-up!">
                                    </form>
                                    </td>
                                    </tr>


    <?php include ( "./inc/footer.inc.php" )?>