尝试填写注册字段时出错


Error when trying to fill in registration fields

我正试图用PHP为学校作业创建一个登录和注册系统,但目前还没有真正起作用。。。

问题是它会产生一个错误,说字段是空的,即使你用数据填充了字段,所以它不应该给出这个错误。

代码:

    <html>
<head>
    <title>Music Database</title>
    <link rel="stylesheet" href="layout.css" type="text/css" />
    <?php
        // Verbinden met de database //
        include('connect.php');
        // Registreer data verkrijgen en in variabelen zetten //
        if(isset($_POST['r_submit'])){
            $r_username = $_POST['r_username']; 
            $r_password = $_POST['r_password'];
            $confirm_r_password = $_POST['confirm_r_password']; 
            $r_name = $_POST['r_name']; 
            $r_surname = $_POST['r_surname'];
            $r_birth = $_POST['r_dateofbirth'];
            $r_mail = $_POST['r_mail'];
        }
    ?>
</head>
<body>
    <div id="login">
        <form name="login" action="login.php" method="post">
            <p>Login: <input class="input" type="text" name="username" value="Username" />
            <input id="password" type="password" name="password" value="Password" /></p>
            <div><a class="link" href="register.php">Register here!</a></div>
            <p align="center"><input class="submit" type="submit" name="login_submit" value="Submit" /></p>
        </form>
    </div>
    <div id="header">
        <a class="link" href="index.php">Music Database</a>
    </div>
    <div id="search">
        <form name="search" action="search.php" method="post">
            <p>Search for: <input class="input" type="text" name="search" value="<?php if(isset($_POST['search'])) print $_POST['search']; ?>" />
            <input class="submit" type="submit" name="search_submit" value="Submit" /></p>
            <p>Artist: <input type="checkbox" name="artistsearch" checked /> 
            Album: <input type="checkbox" name="albumsearch" /> 
            Song: <input type="checkbox" name="songsearch" /> 
            Genre: <input type="checkbox" name="genresearch" /></p>
        </form>
    </div>
    <div id="wrapper"><br /></div>
    <div id="content">
        <h1>Register down here please:</h1>
        <table id="wrap_table">
            <tr>
                <td>
                    <div id="reg_content">
                    <div id="reg_form">
                        <form name="register_login" action="register.php" method="post">
                            <fieldset>
                            <legend>Login Data: </legend>
                                <table class="r_table">
                                    <tr>
                                        <td>Username<sup>*</sup>: </td>
                                        <td><input class="input" type="text" name="r_username" value="<?php if(isset($r_username)){ print $_POST['r_username']; }  ?>"/></td>
                                        <?php
                                            if(isset($_POST['r_submit'])){
                                                if(!isset($r_username)){
                                                    echo "<td>Error: No Username has been entered!</td>";
                                                }
                                            }
                                        ?>
                                    </tr>
                                    <tr>
                                        <td>Password<sup>*</sup>: </td>
                                        <td><input class="input" type="password" name="r_password" value="<?php if(isset($r_password)){ print $_POST['r_password']; }  ?>"/></td>
                                        <?php
                                            if(isset($_POST['r_submit'])){
                                                if(!isset($r_password)){
                                                    echo "<td>Error: No Password has been entered!</td>";
                                                }
                                            }
                                        ?>
                                    </tr>
                                    <tr>
                                        <td>Confirm Password<sup>*</sup>: </td>
                                        <td><input class="input" type="password" name="confirm_r_password"  value="<?php if(isset($confirm_r_password)){ print $_POST['confirm_r_password']; }  ?>"/></td>
                                        <?php
                                            if(isset($confirm_password)){
                                                if($confirm_r_password != $r_password){
                                                    echo "<td>Error: The passwords don't match!</td>";
                                                }
                                            }
                                        ?>
                                    </tr>
                                </table>
                            </fieldset>
                        </form>
                    </div>
                </td>
                <td>
                    <div id="reg_form">
                        <form name="register_personal" action="register.php" method="post">
                            <fieldset>
                            <legend>Personal Data: </legend>
                                <table class="r_table_personal">
                                    <tr>
                                        <td>Name: </td>
                                        <td><input class="input" type="text" name="r_name" value=""/></td>                              
                                    </tr>
                                    <tr>
                                        <td>Surname: </td>
                                        <td><input class="input" type="text" name="r_surname" value=""/></td>
                                    </tr>
                                    <tr>
                                        <td>Date of Birth: </td>
                                        <td><input class="input" type="text" name="r_dateofbirth"  value=""/></td>
                                    </tr>
                                    <tr>
                                        <td>E-mail<sup>*</sup>: </td>
                                        <td><input class="input" type="text" name="r_mail" value="<?php if(isset($r_mail)){ print $_POST['r_mail']; } ?>"/></td>
                                        <?php
                                            if(isset($_POST['r_submit'])){
                                                if(!isset($r_mail)){
                                                    echo "<td>Error: No E-mail has been entered!</td>";
                                                }
                                            }
                                        ?>
                                    </tr>
                                </table>
                            </fieldset>
                        </form>
                    </div>
                </div>
                </td>
            </tr>
            <tr>
                <td>Fields with an asterisk<sup>*</sup> are required for registry!
                    <div id="reg_content">
                        <form name="submit_registry" action="register.php" method="post">
                            <input class="submit" type="submit" name="r_submit" value="Submit!" />
                        </form>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <div id="footerbreak"><br /></div>
    <div id="footer"> &copy; Jorik ter Molen &amp; Camiel Collet, 2011.</div>
</body> 

您的<form action="" method="post">在那里吗?

您还可以调试$_POST全局以查看发生了什么(如果数据正在命中您的脚本)

var_dump($_POST);

所以在代码后面的第二个php文件中包含了第一个"表单"页面,对吗?

首先,在PHP文件中,您应该添加所有

if(isset($_POST['submit'])){

分段为一:

if(isset($_POST['submit'])){
    $r_username = $_POST['r_username'];
    $r_password = $_POST['r_password'];
    $confirm_r_password = $_POST['confirm_r_password']; 
    $r_name = $_POST['r_name']; 
    $r_surname = $_POST['r_surname'];
    $r_birth = $_POST['r_dateofbirth'];
    $r_mail = $_POST['r_mail'];
} 

您的问题很可能在$_POST["submit"]中,因为您有两个检查,一个用于$_POST["submit’],另一个用于$_POST['r_submit’]

除非你双向发送,否则我建议你更改PHP文件以检查"r_submit"

更新:

尝试将表单放入register.php页面,使其看起来像这样:

<?php
    if(isset($_POST['r_submit'])){
        $r_username = $_POST['r_username'];
        $r_password = $_POST['r_password'];
        $confirm_r_password = $_POST['confirm_r_password']; 
    } 
?>
<form action="register.php" method="post">
    <td>Username<sup>*</sup>: </td>
        <td><input class="input" type="text" name="r_username" value="<?php     if(isset($r_username)){ print $_POST['r_username']; }  ?>"/></td>
    <?php
        if(isset($_POST['r_submit'])){
            if(!isset($r_username)){
                echo "<td>Error: No Username has been entered!</td>";
            }
        }
    ?>
</tr>
<tr>
    <td>Password<sup>*</sup>: </td>
    <td><input class="input" type="password" name="r_password" value="<?php if(isset($r_password)){ print $_POST['r_password']; }  ?>"/></td>
    <?php
        if(isset($_POST['r_submit'])){
            if(!isset($r_password)){
                echo "<td>Error: No Password has been entered!</td>";
            }
        }
    ?>
</tr>
<tr>
    <td>Confirm Password<sup>*</sup>: </td>
    <td><input class="input" type="password" name="confirm_r_password"  value="<?php if(isset($confirm_r_password)){ print $_POST['confirm_r_password']; }  ?>"/></td>
    <?php
        if(isset($confirm_password)){
            if($confirm_r_password != $r_password){
                echo "<td>Error: The passwords don't match!</td>";
            }
        }
    ?>
   </tr>
<input name="r_submit" type="submit" value="Submit" />   
</form>

解决方案:

<html>
<head>
    <title>Music Database</title>
    <link rel="stylesheet" href="layout.css" type="text/css" />
    <?php
        // Verbinden met de database //
        include('connect.php');
        // Registreer data verkrijgen en in variabelen zetten //
        if(isset($_POST['r_submit'])){
            $r_username = $_POST['r_username']; 
            $r_password = $_POST['r_password'];
            $confirm_r_password = $_POST['confirm_r_password']; 
            $r_name = $_POST['r_name']; 
            $r_surname = $_POST['r_surname'];
            $r_birth = $_POST['r_dateofbirth'];
            $r_mail = $_POST['r_mail'];
        }
    ?>
</head>
<body>
    <div id="login">
        <form name="login" action="login.php" method="post">
            <p>Login: <input class="input" type="text" name="username" value="Username" />
            <input id="password" type="password" name="password" value="Password" /></p>
            <div><a class="link" href="register.php">Register here!</a></div>
            <p align="center"><input class="submit" type="submit" name="login_submit" value="Submit" /></p>
        </form>
    </div>
    <div id="header">
        <a class="link" href="index.php">Music Database</a>
    </div>
    <div id="search">
        <form name="search" action="search.php" method="post">
            <p>Search for: <input class="input" type="text" name="search" value="<?php if(isset($_POST['search'])) print $_POST['search']; ?>" />
            <input class="submit" type="submit" name="search_submit" value="Submit" /></p>
            <p>Artist: <input type="checkbox" name="artistsearch" checked /> 
            Album: <input type="checkbox" name="albumsearch" /> 
            Song: <input type="checkbox" name="songsearch" /> 
            Genre: <input type="checkbox" name="genresearch" /></p>
        </form>
    </div>
    <div id="wrapper"><br /></div>
    <div id="content">
        <h1>Register down here please:</h1>
        <table id="wrap_table">
            <tr>
                <td>
                    <div id="reg_content">
                    <div id="reg_form">
                        <form name="register_login" action="register.php" method="post">
                            <fieldset>
                            <legend>Login Data: </legend>
                                <table class="r_table">
                                    <tr>
                                        <td>Username<sup>*</sup>: </td>
                                        <td><input class="input" type="text" name="r_username" value="<?php if(isset($r_username)){ print $_POST['r_username']; }  ?>"/></td>
                                        <?php
                                            if(isset($_POST['r_submit'])){
                                                if(!isset($r_username)){
                                                    echo "<td>Error: No Username has been entered!</td>";
                                                }
                                            }
                                        ?>
                                    </tr>
                                    <tr>
                                        <td>Password<sup>*</sup>: </td>
                                        <td><input class="input" type="password" name="r_password" value="<?php if(isset($r_password)){ print $_POST['r_password']; }  ?>"/></td>
                                        <?php
                                            if(isset($_POST['r_submit'])){
                                                if(!isset($r_password)){
                                                    echo "<td>Error: No Password has been entered!</td>";
                                                }
                                            }
                                        ?>
                                    </tr>
                                    <tr>
                                        <td>Confirm Password<sup>*</sup>: </td>
                                        <td><input class="input" type="password" name="confirm_r_password"  value="<?php if(isset($confirm_r_password)){ print $_POST['confirm_r_password']; }  ?>"/></td>
                                        <?php
                                            if(isset($confirm_password)){
                                                if($confirm_r_password != $r_password){
                                                    echo "<td>Error: The passwords don't match!</td>";
                                                }
                                            }
                                        ?>
                                    </tr>
                                </table>
                            </fieldset>
                    </div>
                </td>
                <td>
                    <div id="reg_form">
                            <fieldset>
                            <legend>Personal Data: </legend>
                                <table class="r_table_personal">
                                    <tr>
                                        <td>Name: </td>
                                        <td><input class="input" type="text" name="r_name" value=""/></td>                              
                                    </tr>
                                    <tr>
                                        <td>Surname: </td>
                                        <td><input class="input" type="text" name="r_surname" value=""/></td>
                                    </tr>
                                    <tr>
                                        <td>Date of Birth: </td>
                                        <td><input class="input" type="text" name="r_dateofbirth"  value=""/></td>
                                    </tr>
                                    <tr>
                                        <td>E-mail<sup>*</sup>: </td>
                                        <td><input class="input" type="text" name="r_mail" value="<?php if(isset($r_mail)){ print $_POST['r_mail']; } ?>"/></td>
                                        <?php
                                            if(isset($_POST['r_submit'])){
                                                if(!isset($r_mail)){
                                                    echo "<td>Error: No E-mail has been entered!</td>";
                                                }
                                            }
                                        ?>
                                    </tr>
                                </table>
                            </fieldset>
                    </div>
                </div>
                </td>
            </tr>
            <tr>
                <td>Fields with an asterisk<sup>*</sup> are required for registry!
                  <div id="reg_content">
                            <input class="submit" type="submit" name="r_submit" value="Submit!" />
                            </div>
                      </form>
                </td>
            </tr>
        </table>
    </div>
    <div id="footerbreak"><br /></div>
    <div id="footer"> &copy; Jorik ter Molen &amp; Camiel Collet, 2011.</div>
</body> 

在第82行,您有php检查匹配密码,将其替换为:

<?php
if(isset($_POST['r_submit'])){
    if($confirm_r_password != $r_password)
    {
          echo "<td>Error: The passwords don't match!</td>";
    }
 }
 ?>

尽管这只是一个基本的检查,没有什么安全的,因为用户可以将两个密码都留空,并且它们仍然匹配。