使用PHP和MySQL注册表

时间:2016-06-28 12:39:00

标签: php mysql

我正在学习php和mysql并试图创建一个带有注册和登录页面的表单,但是我无法获得注册表单来将数据写入我的数据库。我没有收到有关数据库连接的错误,但是当我尝试发布数据时,我的表仍然是空的。我收到了错误

  

'注册时出错了。请稍后再试。

非常感谢任何帮助。

<?php
//signup.php
include 'connect.php';

 echo '<h2>Register </h2>';
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
    /*the form hasn't been posted yet, display it
      note that the action="" will cause the form to post to the same page it is on */
    echo '<form method="post" action="">
        Username: <input type="text" name="Username" />
        Password: <input type="password" name="Password">
        Confirm Password: <input type="password" name="Confirm">

        <input type="submit" value="Submit" />
     </form>';

    }
else
{
    /* so, the form has been posted, we'll process the data in three steps:
        1.  Check the data
        2.  Let the user refill the wrong fields (if necessary)
        3.  Save the data 
    */
    $errors = array(); /* declare the array for later use */

    if(isset($_POST['Username']))
    {
        //the user name exists
        //if(!ctype_alnum($_POST['Username']))
        if($_POST['Username'] == ['Username'])  
        {
            $errors[] = 'The username is already in use.';
        }

        }
    else
    {
        $errors[] = 'The username field must not be empty.';
    }

    if(isset($_POST['Password']))
    {
        if($_POST['Password'] != $_POST['Confirm'])
        {
            $errors[] = 'The two passwords did not match.';
        }
    }
    else
    {
        $errors[] = 'The password field cannot be empty.';
    }

    if(!empty($errors)) /*check for an empty array, if there are errors, they're in this array (note the ! operator)*/
    {
        echo 'Uh-oh.. a couple of fields are not filled in correctly..';
        echo '<ul>';
        foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */
        {
            echo '<li>' . $value . '</li>'; /* this generates a nice error list */
        }
        echo '</ul>';
    }
    else
    {
        //the form has been posted without errors, so save it
        //notice the use of mysql_real_escape_string, keep everything safe!
        //also notice the sha1 function which hashes the password
        $sql = "INSERT INTO
                    Users(Username, Password)
                VALUES('" . mysql_real_escape_string($_POST['Username']) . "',
                       '" . md5($_POST['Password']) . "',
                        NOW(),
                        0)";

        $result = mysql_query($sql);
        if(!$result)
        {
            //something went wrong, display the error
            echo 'Something went wrong while registering. Please try again later.';
            //echo mysql_error(); //debugging purposes, uncomment when needed
        }
        else
        {
         header ("location: index.htm"); //redirects to Index Page


        }
    }
}

?> 

谢谢

1 个答案:

答案 0 :(得分:1)

试试这个。我在Users表字段

中添加了回拨号
  $username = mysql_real_escape_string($_POST['Username']);
  $password = md5($_POST['Password']);


  $sql= "INSERT INTO  Users(`Username`,`Password`) VALUES('$username',   '$password')";

您正在插入NOW()和0.2个额外值

注意调试SQL查询的第一步是先在MySQL中运行它。因此,首先尝试在MySQL中首先使用UsernamePassword的虚拟值运行SQL语句,看看它是否有效