在 PHP 的后变量中未定义的数据验证索引(注册.php)


Data validation index undefined within post variables in PHP (registration.php)

我有一些不太明白的错误。 以下错误是:

Notice: Undefined index: newUser in C:'xampp'htdocs'assignment_2'registration.php on line 24
Notice: Undefined index: newUser in C:'xampp'htdocs'assignment_2'registration.php on line 63
Notice: Undefined index: newUser in C:'xampp'htdocs'assignment_2'registration.php on line 67
Notice: Undefined index: newUser in C:'xampp'htdocs'assignment_2'registration.php on line 71

这很奇怪,因为我发誓昨天错误不存在,我还在 netbeans 中进行了历史记录比较,它显示了相同的:(

这是我的注册.php如下所示:

<html>
    <h4>
        <center>
            New User Registration
        </center>
    </h4>
    <body>
        <?php
        /*
         * What this code does, is it takes the users registration information, and stores it in a file.
         * After that what I hope to do is to retrive the same file when the user is on the login page and if 
         * the login matches whats in the file, then proceed to the invoice page.
         */
include "functions.inc";
    // Define the datafile to hold the $users array
    $datafile = "users.dat";
    // See if the user has submitted something
    if (array_key_exists('register', $_POST))
    {
        // Get the new user info
            $the_user = $_POST['newUser']['ID'];

// Load the file of users and store it in $users
        $users = arrayfile_to_array($datafile);

        // Validate user name and password
        if (user_exists($the_user['ID'], $users))
        {
            echo "<p><center>Please fill in all text boxes</center></p>";
        }
        else
        {
            // If valid, save to the file of users
            $users[] = $the_user;
            array_to_arrayfile($users, $datafile);
        }
    }
    else
    {
        if (!file_exists($datafile))  // Data file doesn't exist, so create it
        {
            $users = array();
            array_to_arrayfile($users, $datafile);
        }
    }
?>
        <?php
       // my defined error values 
      $errEmail    = "";
      $errUser     = "";
      $errPass     = "";

      if(isset($_POST["register"])){
        // User must be digits and letters
        if(preg_match("/^[0-9a-zA-Z]{5,}$/", $_POST['newUser']['ID']) === 0)
          $errUser = '<span class="error">Username must be more than 5 characters and contain letters and numbers.</span>';
        // Password must be strong
        if(preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST['newUser']['password']) === 0)
          $errPass = '<span class="error">Password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit</span>';
        //Email validation
        if(preg_match("/^[a-zA-Z]'w+('.'w+)*'@'w+('.[0-9a-zA-Z]+)*'.[a-zA-Z]{2,4}$/", $_POST['newUser']['email']) === 0)
          $errEmail = '<span class="error">example: chars(.chars)@chars(.chars).chars(2-4)</span>';
      }  
  ?>
        <form action = "invoice.php" method= 'get'> 
            <center>
                <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
                    <tr>
                        <td>Username</td>
                        <td>:</td>
                        <td ><input name="newUser[ID]" type="text" size="16" value="">
          <?php  if(isset($errUser) and $errUser !='') echo $errUser; ?>
                        </td >
                    </tr>
                     <tr>
                        <td>Password</td>
                        <td>:</td>
                        <td><input name="newUser[password]" type="password" size="16" value="">
          <?php  if(isset($errPass) and $errPass !='') echo $errPass; ?>
                        </td >
                    </tr>
                                         <tr>
                        <td>Email</td>
                        <td>:</td>
                        <td><input name="newUser[email]" type="text" size="50" value="">
          <?php  if(isset($errEmail) and $errEmail !='') echo $errEmail; ?>
                        </td >
                    </tr> 
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td> 
                    </tr>          
                </table>
                <input type='submit' name='register' value='Register'><br>
            </center>    
        </form>
    </body>
</html>

任何帮助将不胜感激。 谢谢!

未定义的索引表示newUser不会在$_POST超全局中发送。它仍然在你的形式中吗?

示例表单指向invoice.php但您说这是registration.php

此示例表单通过 method=get 发送,这将是$_GET

您是否更改了$_POST变量?这个片段是最重要的线索:

// See if the user has submitted something
if (array_key_exists('register', $_POST))
{
    // Get the new user info
    $the_user = $_POST['newUser']['ID'];

这是检查$_POST['register']是否存在,但正在从$_POST['newUser']中提取真值。 所以也许这个:

if (array_key_exists('register', $_POST))

应该改为这个:

if (array_key_exists('newUser', $_POST))

或者像这样发生的变量设置:

$the_user = $_POST['newUser']['ID'];

应该改为这个:

$the_user = $_POST['register']['ID'];

但似乎registernewUser在您的$_POST和一般代码逻辑中混淆了。

由于这是一个赋值 - 在您的包含语句之后插入 var_dump($_POST);

<?php
/*
 * What this code does, is it takes the users registration information, and stores it in a file.
* After that what I hope to do is to retrive the same file when the user is on the login page and if 
* the login matches whats in the file, then proceed to the invoice page.
*/
include "functions.inc";
var_dump( $_POST );
// Define the datafile to hold the $users array
 $datafile = "users.dat";

然后根据需要调整代码。