PHP注册表单不起作用

时间:2017-02-01 17:31:06

标签: php forms

我想使用PHP / MYSQL制作一个简单的注册表单。但是,当我提交表单时,它不会将用户添加到数据库中。如果有人能找到解决方案,我们会很高兴。这是我的HTML:

<form action="signup.php" method="POST">

<label>Pick a username: </label><input type="text" name="username" /> <br>
  <label>Pick a password: </label><input type="password" name="password" /> <br>
  <label>Verify password: </label><input type="password" name="verify_password" /> <br>
  <label>Email address: </label><input type="text" name="email" /> <br>
  <button type="submit">Sign Up</button>

</form>

并且signup.php:

<?php
include 'db.php';

$username = $_POST['username'];
$password = $_POST['password'];
$verify_password = $_POST['verify_password'];
$email = $_POST['email'];

 if ($verify_password !== $password) {
    echo "You didn't type your password correctly!";

} else {
    $sql = "INSERT INTO users (username, password, verify_password, email) 
    VALUES ('$username', '$password', '$verify_password', '$email')";

    $rezultat = mysqli_query($link, $sql);

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

编辑:这是db.php:

<?php
define ("MYSQL_HOST", "localhost");
define ("MYSQL_USER", "root");
define ("MYSQL_PASS", "");
define ("MYSQL_DBNAME", "registrationTest");

function connect() {
    global $link;

    $link = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DBNAME) or die ("Connection error: " . mysqli_connect_error);
}
?>

2 个答案:

答案 0 :(得分:1)

看到您在编辑中添加了连接文件:

您的连接文件包含一个名为connect()的自定义函数,但您从未调用它。

function connect() {
    global $link;

    $link = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DBNAME) or die ("Connection error: " . mysqli_connect_error);
}

要么删除:

function connect() {
    global $link;

}

或者在脚本中调用您希望它发生的函数。

即:

<?php
include 'db.php';

echo connect();

// ... rest of your code
  • 注意:我不会重复关于代码安全性的说法。

编辑:我也不会对准备好的声明进行全部重写并使用安全密码散列函数;它我的工作。

  • 我&#34;回答&#34;问题,期间

但是,我会将学习的参考资料包含在内,并自行完成:

更改以下内容以检查结果是否成功。如果没有,请返回错误,如果有错误:

else {
    $sql = "INSERT INTO users (username, password, verify_password, email) 
    VALUES ('$username', '$password', '$verify_password', '$email')";

    $rezultat = mysqli_query($link, $sql);

    header("Location: index.php");
}

为:

else {
    $sql = "INSERT INTO users (username, password, verify_password, email) 
    VALUES ('$username', '$password', '$verify_password', '$email')";

    $rezultat = mysqli_query($link, $sql);
}

if($rezultat){

    header("Location: index.php");
    exit;

}else{
    echo "Error: " . mysqli_error($link);
}

答案 1 :(得分:-2)

下面是一个简化的代码,您只需编辑数据库值即可为任意数量的项目重复使用任何次数。

下面应该是你的db.php文件

<?php
    function db_connect () {
        $link = @mysqli_connect ( 'localhost', 'username', 'password', 'table_name' );

        if ( mysqli_connect_errno () )
            die ( 'Failed to connect to server.' );
        return $link;
    }
?>

现在可以在任何.php文件中简单地调用此文件。在您的情况下,它是注册脚本。在此之前,下面应该是您的HTML。

<form action="signup.php" method="POST">

<label>Pick a username: </label><input type="text" name="username" /> <br>

//Note that I have added "ID" to Password and Verify Password. This will be used to match the passwords before even submit.  
<label>Pick a password: </label><input type="password" id="password" name="password" /> <br>
  <label>Verify password: </label><input type="password" id="verify_password" name="verify_password" /> <br>
  <label>Email address: </label><input type="text" name="email" /> <br>

//Note that I have added "Name" to the Button. This will be used in the sign-up script for validation. The script is called by onclick where Javascript fucntion is called.
<button name="singup-details" type="submit" onclick="registerFunction()">Sign Up</button>

</form>

建议通过Javascript而不是PHP注册脚本来匹配密码。这会阻止用户提交表单,以防密码不匹配。

以下是您可以在</body>代码之前过去的Javascript代码。

<script type="text/javascript">
    //This will set passwordmatch variable on password and verify_password change.
    $('#password, #verify_password').on('keyup', function () {
    if ($('#password').val() == $('#verify_password').val()) {
      $('#message').html('').css('color', 'green');

      passwordmatch = 'yes';
    } else {
      $('#message').html(' Enter the Password again. Both the fields must match.').css('color', 'red');

      passwordmatch = 'no';
    }
});

//If password matches, it will prompt to ensure that if the entries are correct. Or else, will set the focus to Password field.
    function registerFunction() {
        if (passwordmatch == 'yes') {
          if (confirm('You are about to change your password.\nPress OK to proceed.')) return false;

          $( "#userpassword" ).focus();
          $( "#userpassword" ).select();

          event.preventDefault();
        } else {
          alert('Password do not match. Please check and try again.');

          $( "#userpassword" ).focus();
          $( "#userpassword" ).select();

          event.preventDefault();
        }
    }
</script>

现在终于是你需要改变的signup.php。

<?php
    session_start();
    ob_start();

    //You can set timezone in order if you plan to store the registration time as well.
    date_default_timezone_set('Asia/Dubai');

    include 'db.php';


    $username = $_POST['useremail'];

    $password = $_POST['password'];

    $email = $_POST['email'];

    //Saving the password directly is not safe. It is better to encrypt it using, at least, SHA1. Below is the way to encrypt the password.
    $password = sha1($_POST['userpassword']);

    //By checking isset $_POST of the singup button of HTML using its name value, we ensure that the user can browse this page only by filling in the Singup form or else will be sent back to the Signup form.
    if (isset($_POST['singup-details'])) {
        //This is the way you can check that if the user exists or not.
        $sql = "SELECT * FROM your_table WHERE email = '" . $email . "'";
        $result = mysqli_query(db_connect(), $sql);

        $num_row = mysqli_num_rows($result);

        if( $num_row >= 1 ) {
            //If username exists, it will take back to the singup form.
            ?>
                <script language="javascript" type="text/javascript">
                    alert('<?php echo $username; ?> already exists in our records.\nTry Again.');
                    window.location = 'your_signup_form.php';
                </script>
            <?php
        }
        else {
            //If username doesn't exist it will save it into the database. Also note that you do not have to save the Verify Password. It is not required at all.
            $sql1 = "INSERT INTO table_name (username, email, password)
            VALUES ('$username', '$email', '$password')";

            $result1 = mysqli_query ( db_connect(), $sql1 );

            ?>
                <script language="javascript" type="text/javascript">
                    alert('Thank you <?php echo $username; ?> for registering.');
                    window.location = 'any_page_you_want.php';
                </script>
            <?php
        }
    }
    else {
        //In case someone tries to access this page directly, this will take him to the singup page instead.
        ?>
        <script language="javascript" type="text/javascript">
            window.location = 'your_signup_form.php';
        </script>
        <?php
    }
?>

希望这会有所帮助。快乐的编码...