1

以下是我网站上的 register.php 代码。此代码允许用户注册我的网站,在单击提交按钮时为用户名、电子邮件、密码等创建一个 MySQL 条目。

该按钮名为“reg”并使用 $_POST。单击提交按钮后,PHP 代码将运行多个if语句,以确保用户输入的信息有效且不预先存在。

如果用户存在,或者提交时出错,它会设置 PHP 变量$errormessage并应该将其回显。现在,我的提交按钮不像被按下一样。没有错误消息,没有插入 SQL 行,什么都没有。

        <?php
            if( $_POST['reg']){

                /* Make sure values are correct and valid */
                $getuser = $_POST['user'];
                $getemail = $_POST['email'];
                $getpass = $_POST['password'];
                $getrepass = $_POST['retypepassword'];

                /* Check to see if username entererd */
                if($getuser){

                    /* Check to see if email entererd */
                    if($getemail){

                        /* Check to see if password entererd */
                        if($getpass){

                            /* Check to see if retyped password entererd */
                            if($getrepass){

                                /* Check to see if passwords are the EXACT same */
                                if($getpass === $getrepass){

                                    /* Check to see if VALID email is entered */
                                    if( (strlen($getemail) >= 7) &&
                                        (strstr($getemail, "@")) &&
                                        (strstr($getemail, ".")) ){

                                        /* Email is valid mysql query */
                                        require ("./connect.php");

                                        $query = mysql_query("SELECT * FROM users WHERE username ='$getuser'");

                                        /* If mysql returns zero, the user does not exist. */
                                        $numrows = mysql_num_rows($query);

                                        /* Check if email exists */
                                        if($numrows == 0) {
                                            $query = mysql_query("SELECT * FROM users WHERE email ='$getemail'");
                                            $numrows = mysql_num_rows($query);
                                            if($numrows == 0){
                                                $date = date("F d, Y");
                                                $code = md5(rand());

                                                mysql_query("INSERT INTO users VALUES ('', '$getuser', '$getpass', '$getemail', '0', '$code', '$date')");

                                                $query = mysql_query ("SELECT ALL * FROM users WHERE username = '$getuser'");
                                                $numrows = mysql_num_rows($query);

                                                /* Check to make user was generated */
                                                if($numrows == 1){
                                                    $site = "http://www.midnightnachos.com/gs";
                                                    $webmaster = "universitydb@gmail.com";
                                                    $headers = "From: $webmaster";
                                                    $subject = "Activate Your Account";
                                                    $message = "Thanks for registering. Click the link below to activate your account.\n";
                                                    $message .= "$site/activate.php?user=$getuser&code=$code\n";
                                                    $message .= "You must activate your account to login.";

                                                    if (mail($getemail, $subject, $message, $headers)){
                                                        $errormessage = "You have been registered. You must activate your account from the activation link sent to your email.";
                                                        echo $form;
                                                        $getuser = "";
                                                        $getpass = "";
                                                    }
                                                    else
                                                        echo "An error has occured. Your activation email was not sent.";
                                                }
                                                else
                                                    $errormessage = "An error has occurred. Account not created.";
                                            }
                                            else
                                                $errormessage = "Email address already in use.";
                                        }
                                        else
                                            $errormessage = "Username already exists.";

                                        mysql_close;
                                    }
                                    else
                                        $errormessage = "You did not enter a valid email.";
                                }
                                else
                                    $errormessage = "Your passwords did not match.";
                            }
                            else
                                $errormessage = "You must retype your password.";
                        }
                        else
                            $errormessage = "You must enter your password.";
                    }
                    else
                        $errormessage = "You must enter an email to register.";
                }
                else
                    $errormessage = "You must enter a username to register.";

                echo $form;
            }

            $form = "
                <div class='splash'>
                  <h1>Register for Game Swap</h1>
                  <p>Register for Game Swap to browse what games other local
                     users have added to their library. Propose trades,
                     chat, and meet to swap games. Your email address
                     will only be used to notify you when someone has
                     sent a trade offer. No newsletters, advertisements or
                     updates will be sent by us. We will also never sell
                     your contact information to third parties.</p>
                  <br />
                  <p align='center'>Fill out the form below to get started</p>
                  <br />
                  <form align='center' action='./register.php' method='POST'>
                      <input type='text' name='user' value='$getuser' class='box' size='30' placeholder='Username' /><br />
                      <input type='password' name='password' class='box' size='30' placeholder='Password' /><br />
                      <input type='password' name='retypepassword' class='box' size='30' placeholder='Retype Password' /><br />
                      <input type='text' name ='email' value='$getemail' class='box' size='30' placeholder='Email Address' /><br />
                      <input type='button' name='reg' class='loginbutton' value='Register' /><br />
                  </form>
                </div>
                <br/> $errormessage";

            echo $form;
        ?>

    </body>
</html>
4

1 回答 1

0

我认为您混淆了按钮的 type 属性,即它不是button,而是submit

所以,我猜你有一个普通的文本输入字段,但是你的 CSS 欺骗了你的眼睛。试着写进去:)

要通过按钮提交表单,您可以使用:

<input type="submit" name="reg" value="Register!"/>

<button name="reg" value="1-or-anything">Register!</button>

至于可能的不同编码方式(一次获取所有验证错误):

$error_list = array();
if ($condition1) $error_list[] = 'My Error message 1';
if ($condition2) $error_list[] = 'My Error message 2';
if ($condition3) $error_list[] = 'My Error message 3';
...
if (empty($error_list)) the_fun_part();
else {
    foreach($error_list as $msg)
        echo "{$msg}<br/>";
}
于 2013-08-08T21:22:03.960 回答