【问题标题】:php form disappearsphp表单消失
【发布时间】:2012-10-26 11:21:56
【问题描述】:

在我添加 php.ini 之前,我有一个可以完美显示的会员注册表单。一旦添加了 php 代码,表单就会完全消失。我已经尝试了一切,但我认为在这个阶段需要一套新的眼睛。 任何帮助将不胜感激。干杯。

<?php

if ( $_POST['registerbtn'] ) {
    $getuser = $_POST['user'];
    $getemail = $_POST['email'];
    $getpass = $_POST['pass'];
    $getretypepass = $_POST['retypepass'];

    if ($getuser) {
        if ($getemail) {
            if($getpass) {
                if ($getretypepass) {
                    if ($getpass === $getretypepass) {
                        if ( (strlen($getemail) >= 7) && (strstr($getemail, "@")) && (strstr($getemail, "."))) {
                            require("./connect.php");

                            $query = mysql_query("SELECT * FROM users WHERE username='$getuser'");
                            $numrows = mysql_num_rows($query);
                            if ($numrows == 0){
                                    $query = mysql_query("SELECT * FROM users WHERE email='$getemail'");
                                    $numrows = mysql_num_rows($query);
                                    if ($numrows == 0){

                                        $password = md5($password);
                                        $date = date("F d, Y");
                                        $code = md5(rand());

                                        mysql_query("INSERT INTO users VALUES (
                                            '', '$getuser', '$getpassword', '$getemail', '0', '$getcode', '$getdate'
                                        )");



                                        $query = mysql_query("SELECT * FROM users WHERE username='$getusername'");
                                        $numrows = mysql_num_rows($query);
                                        if ($numrows == 1) {

                                            $site ="http://localhost/member.php";
                                            $webmaster = "Bror Phren <bmdoublec@hotmail.com>";
                                            $headers = "From: $webmaster";
                                            $subject = "Activate your account";
                                            $message = "Thanks for registering. Click the link below to activate your account";
                                            $message .= "$site/activate.php?user=$getuser&code=$code\n";
                                            $message .= "You must activate your account t login.";

                                            if (mail($getemail, $subject, $message, $headers) ) {
                                                $errormsg = "You have been registered. You must activate your account from the activation link sent to <b>$getemail</b> ";
                                                $getuser = "";
                                                $getemail = "";
                                            }
                                            else
                                                $errormsg = "An error has occured. Your activation email was not sent";


                                        }
                                        else
                                            $errormsg = "An error has occured. Your account was not created.\n";


                            }
                            else
                                $errormsg = "There is already a user with that email";

                            }
                            else
                                $errormsg = "There is already a user with that username";

                            mysql_close();


                        }
                        else
                        $errormsg = "You must enter a valid email address to register.";

                }
                else
                $errormsg = "You must retype your password to register.";

            }
            else
            $errormsg = "You must enter password to register.";

        }
        else
            $errormsg = "You must enter email to register.";

    }
    else
        $errormsg = "You must enter username to register.";

}

$form = "<form action='./register.php' method='post'>
<table>

    <tr>
    <td></td>
    <td><font color='red'>$errormsg</font></td>
    </tr>
    <tr>

    <td>Username:</td>
    <td><input type='text' name='user' value='$getuser' /></td>
    </tr>

    <tr>
    <td>Email:</td>
    <td><input type='text' name='email' value='$getemail' /></td>
    </tr>

    <tr>
    <td>Password:</td>
    <td><input type='password' name='pass' value='' /></td>
    </tr>

        <tr>
    <td>Retype:</td>
    <td><input type='password' name='retypepass' value='' /></td>
    </tr>

        <tr>
    <td>Retype:</td>
    <td><input type='submit' name='registerbtn' value='Register' /></td>
    </tr>

    </table>
</form>";

echo $form;

}

?>

【问题讨论】:

标签: php sql forms login registration


【解决方案1】:

您的 echo 语句位于 if ( $_POST['registerbtn'] ) {} 块中,这意味着如果未提交表单,则不会显示该表单。

【讨论】:

  • 这就解释了是的。将表单移到 if 语句之外,并使用 isset 检查并查看是否在打印之前设置了 $errormsg$getuser$getemail,例如if(isset($errormsg) { echo $errormsg;}
【解决方案2】:

我认为你需要使用 else

<?php

if (isset($_POST['registerbtn'])) {
    $getuser = $_POST['user'];
    $getemail = $_POST['email'];
    $getpass = $_POST['pass'];
    $getretypepass = $_POST['retypepass'];

    if ($getuser) {
        if ($getemail) {
            if($getpass) {
                if ($getretypepass) {
                    if ($getpass === $getretypepass) {
                        if ( (strlen($getemail) >= 7) && (strstr($getemail, "@")) && (strstr($getemail, "."))) {
                            require("./connect.php");

                            $query = mysql_query("SELECT * FROM users WHERE username='$getuser'");
                            $numrows = mysql_num_rows($query);
                            if ($numrows == 0){
                                    $query = mysql_query("SELECT * FROM users WHERE email='$getemail'");
                                    $numrows = mysql_num_rows($query);
                                    if ($numrows == 0){

                                        $password = md5($password);
                                        $date = date("F d, Y");
                                        $code = md5(rand());

                                        mysql_query("INSERT INTO users VALUES (
                                            '', '$getuser', '$getpassword', '$getemail', '0', '$getcode', '$getdate'
                                        )");



                                        $query = mysql_query("SELECT * FROM users WHERE username='$getusername'");
                                        $numrows = mysql_num_rows($query);
                                        if ($numrows == 1) {

                                            $site ="http://localhost/member.php";
                                            $webmaster = "Bror Phren <bmdoublec@hotmail.com>";
                                            $headers = "From: $webmaster";
                                            $subject = "Activate your account";
                                            $message = "Thanks for registering. Click the link below to activate your account";
                                            $message .= "$site/activate.php?user=$getuser&code=$code\n";
                                            $message .= "You must activate your account t login.";

                                            if (mail($getemail, $subject, $message, $headers) ) {
                                                $errormsg = "You have been registered. You must activate your account from the activation link sent to <b>$getemail</b> ";
                                                $getuser = "";
                                                $getemail = "";
                                            }
                                            else
                                                $errormsg = "An error has occured. Your activation email was not sent";


                                        }
                                        else
                                            $errormsg = "An error has occured. Your account was not created.\n";


                            }
                            else
                                $errormsg = "There is already a user with that email";

                            }
                            else
                                $errormsg = "There is already a user with that username";

                            mysql_close();


                        }
                        else
                        $errormsg = "You must enter a valid email address to register.";

                }
                else
                $errormsg = "You must retype your password to register.";

            }
            else
              $errormsg = "You must enter password to register.";

        }
        else
            $errormsg = "You must enter email to register.";
    }
    else
        $errormsg = "You must enter username to register.";
}
}

这里

else{ 
$form = "<form action='./register.php' method='post'>
<table>

    <tr>
    <td></td>
    <td><font color='red'>$errormsg</font></td>
    </tr>
    <tr>

    <td>Username:</td>
    <td><input type='text' name='user' value='$getuser' /></td>
    </tr>

    <tr>
    <td>Email:</td>
    <td><input type='text' name='email' value='$getemail' /></td>
    </tr>

    <tr>
    <td>Password:</td>
    <td><input type='password' name='pass' value='' /></td>
    </tr>

        <tr>
    <td>Retype:</td>
    <td><input type='password' name='retypepass' value='' /></td>
    </tr>

        <tr>
    <td>Retype:</td>
    <td><input type='submit' name='registerbtn' value='Register' /></td>
    </tr>

    </table>
</form>";

echo $form;

}

?>

【讨论】:

  • 没关系的人。如果您认为我的答案解决了问题,请接受答案..
【解决方案3】:

您在 PHP 代码中存在错误,导致任何程序无法运行。

我建议通过 PHPLint 之类的工具运行您的代码以查看错误。

此外,您可能需要考虑以更易于阅读和维护的方式构建代码。

例如,您可以避免所有那些嵌套的 if 语句并将其写为:

if (!$getuser) {
   $errormsg = "You must enter username to register.";
}
else if (!$getemail) {
   $errormsg = "You must enter email to register.";
}
...

【讨论】:

    【解决方案4】:

    编辑:正如 Armon Toubman 所述,只有在相关数据已发布到页面时才会显示该表单。您需要将表单移到 if 语句之外。请参阅下面的代码:

    <?php
    
    if ( $_POST['registerbtn'] ) {
        $getuser = $_POST['user'];
        $getemail = $_POST['email'];
        $getpass = $_POST['pass'];
        $getretypepass = $_POST['retypepass'];
    
        if ($getuser) {
            if ($getemail) {
                if($getpass) {
                    if ($getretypepass) {
                        if ($getpass === $getretypepass) {
                            if ( (strlen($getemail) >= 7) && (strstr($getemail, "@")) && (strstr($getemail, "."))) {
                                require("./connect.php");
    
                                $query = mysql_query("SELECT * FROM users WHERE username='$getuser'");
                                $numrows = mysql_num_rows($query);
                                if ($numrows == 0){
                                        $query = mysql_query("SELECT * FROM users WHERE email='$getemail'");
                                        $numrows = mysql_num_rows($query);
                                        if ($numrows == 0){
    
                                            $password = md5($password);
                                            $date = date("F d, Y");
                                            $code = md5(rand());
    
                                            mysql_query("INSERT INTO users VALUES (
                                                '', '$getuser', '$getpassword', '$getemail', '0', '$getcode', '$getdate'
                                            )");
    
    
    
                                            $query = mysql_query("SELECT * FROM users WHERE username='$getusername'");
                                            $numrows = mysql_num_rows($query);
                                            if ($numrows == 1) {
    
                                                $site ="http://localhost/member.php";
                                                $webmaster = "Bror Phren <bmdoublec@hotmail.com>";
                                                $headers = "From: $webmaster";
                                                $subject = "Activate your account";
                                                $message = "Thanks for registering. Click the link below to activate your account";
                                                $message .= "$site/activate.php?user=$getuser&code=$code\n";
                                                $message .= "You must activate your account t login.";
    
                                                if (mail($getemail, $subject, $message, $headers) ) {
                                                    $errormsg = "You have been registered. You must activate your account from the activation link sent to <b>$getemail</b> ";
                                                    $getuser = "";
                                                    $getemail = "";
                                                }
                                                else
                                                    $errormsg = "An error has occured. Your activation email was not sent";
                                            }
                                            else
                                                $errormsg = "An error has occured. Your account was not created.\n";
                                }
                                else
                                    $errormsg = "There is already a user with that email";
                                }
                                else
                                    $errormsg = "There is already a user with that username";
                            }
                            else
                            $errormsg = "You must enter a valid email address to register.";
                    }
                    else
                    $errormsg = "You must retype your password to register.";
                }
                else
                $errormsg = "You must enter password to register.";
            }
            else
                $errormsg = "You must enter email to register.";
    
        }
        else
            $errormsg = "You must enter username to register.";
        }
        mysql_close();
    }
    ?>
    <form action='./register.php' method='post'>
    <table>
    
        <tr>
          <td></td>
          <td><font color='red'><?php if(isset($errormsg){ echo $errormsg; } ?></font></td>
        </tr>
    
        <tr>    
          <td>Username:</td>
          <td><input type='text' name='user' value='<?php if(isset($getuser){ echo $getuser; } ?>' /></td>
        </tr>
    
        <tr>
          <td>Email:</td>
          <td><input type='text' name='email' value='<?php if(isset($getemail){ echo $getemail; } ?>' /></td>
        </tr>
    
        <tr>
          <td>Password:</td>
          <td><input type='password' name='pass' value='' /></td>
        </tr>
    
        <tr>
          <td>Retype:</td>
          <td><input type='password' name='retypepass' value='' /></td>
        </tr>
    
        <tr>
          <td>Retype:</td>
          <td><input type='submit' name='registerbtn' value='Register' /></td>
        </tr>
    
        </table>
    </form>
    

    【讨论】:

    • 为了清楚起见,我还移动了 MySQL 关闭语句。正如宇智波斑对您的问题所评论的那样,您也应该考虑使用准备好的陈述。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多