【问题标题】:Notice: Undefined index: username in C:\xampp\htdocs\Registration\scripts\validate.php on line 6 [duplicate]注意:未定义索引:第 6 行 C:\xampp\htdocs\Registration\scripts\validate.php 中的用户名 [重复]
【发布时间】:2014-06-10 19:22:06
【问题描述】:

我正在尝试通过 php 脚本register.php 将表单数据发布到数据库来注册新用户,但是当我点击注册时出现一系列错误,数据应该由第二个脚本验证叫validate.php。我的register.php 如下所示。表单为空和填写时存在相同的错误。

<?php require('scripts/validate.php');?>
<!DOCTYPE html>
<html>
<head>

  <meta charset="UTF-8" />
  <title>Register</title>

<body>
 <div id="mainWrapper">
    <div id="register">
        <?php if(isset($error)){echo "<div id='error'>".$error."</div>";}?>
        <?php if(isset($success)){echo "<div id='success'>".$success."</div>";}?>
      <form method="post" action="" >
        <fieldset>
            <legend>Register Here</legend>
            <p>
            <label for="Username">Username</label>
            <input type="text" id="username"/>
            </p>
            <p>
            <label for="Email">Email</label>
            <input type="text" id="email"/>
            <p>
            <label for="Firstname">Firstname</label>
            <input type="text" id="firstname"/>
            </p>
            <p>
            <label for="Lastname">Lastname</label>
            <input type="text" id="lastname"/>
            </p>
            <p>
            <label for="Password">Password</label>
            <input type="password" id="password"/>
            </p>
            <p>
            <label for="Re-Type Password">Re-Type Password</label>
            <input type="password" id="password2"/>
            </p>
            <p>
            <label for="DOB">DOB</label>
            <input type="text" id="dob">
            </p>
            <p>
            <label for="Adress">Adress</label>
            <input type="text" id="address"/>
            </p>
            <p>
            <label for="Adress 2">Adress 2</label>
            <input type="text" id="address2"/>
            </p>
            <p>
            <label for="town">Town</label>
            <input type="text" id="town"/>
            </p>
            <p>
            <label for="county">County</label>
            <input type="text" id="county"/>
            </p>
            <p>
            <label for="Postalcode">PostalCode</label>
            <input type="text" id="postcode"/>
            </p>
            <p>
            <label for="contactno">Contact No.</label>
            <input type="text" id="contact"/>
            </p>
          <input type="submit" name="submit" value="Register"/>
            </fieldset>
            </form>
            </div>
        </div>
</body>
</html>

而 validate.php 在这里

   <?php include('connection.php');?>
    <?php
    if(isset($_POST['submit']))
    {

    $username=$_POST['username'];
    $email=$_POST['email'];
    $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $password=$_POST['password'];
    $password2=$_POST['password2'];
    $dob=$_POST['dob'];
    $address=$_POST['address'];
    $address2=$_POST['address2'];
    $town=$_POST['town'];
    $county=$_POST['county'];
    $postcode=$_POST['postcode'];
    $contact=$_POST['contact'];

    $fetch=mysql_query("SELECT id FROM users WHERE email='$email'")or die(mysql_error());
    $num_rows=mysql_num_rows($fetch);

    if(empty($username)||empty($email) || empty($firstname) || empty($lastname) || empty($password) || empty($password2)  || empty($dob) || empty($address) || empty($town)|| empty($postcode) || empty($contact))
    {
        $error= 'ALl * fields are required';
    }
    elseif (!filter_var($email,FILTER_VALIDATE_EMAIL)) 
    {
    $error= 'A valid email is required';
    }
    elseif (!empty($contact)) 
    {
       if (!is_numeric($contact)) 
         {
           $error= 'Enter a valid contact No.';
         }  
    }
    elseif ($password !=$password2) 
    {
    $error= "Passwords don't match";
    }
    elseif ($num_rows >=1)
     {
        $error='We already have this email registered,try a new one!';
     }
    else
        {
            $password=md5($password);
            $sql=mysql_query("INSERT INTO user(username,email,firstname,lastname,password,dob,address,address2,town,county,postcode,contact)VALUES('$username','$email','$firstname','$lastname','$password','$dob','$address','$address2','$town','$county','$postcode','$contact')");
        if($sql)
        {
    header("location:login.php");   
        }
       }
}
?>

我会非常感谢你们。

【问题讨论】:

  • 没有name,没有值...
  • 谢谢大家,我消除了这些错误。但是我仍然有一个问题,表单没有添加到数据库中,并且从连接脚本(connection.php)中收到此错误已弃用:mysql_connect():不推荐使用mysql扩展并将在将来删除:使用mysqli 或 PDO 在第 7 行的 C:\xampp\htdocs\Registration\scripts\connection.php 中。这里是连接脚本

标签: php post


【解决方案1】:

给你的inputs 一个name 属性。

例如:

<input type="text" id="username" name="username" />

问题是它正在寻找name,但它不存在。

这适用于您的所有 input 字段,不仅仅是那个特定的字段,也不仅仅是 type="text"。

【讨论】:

    【解决方案2】:

    当您使用表单发布数据时,您需要使用name 属性指定数据的名称。用name 替换所有id 属性,您的表单就可以工作了。例如:

    &lt;input type="text" id="username"/&gt;

    应该变成:

    &lt;input type="text" name="username"/&gt;

    【讨论】:

      【解决方案3】:

      将所有元素的 id 替换为名称。提交表单时,元素的信息与关联的名称一起提交,而不是 id。 例如&lt;input type="text" id="username" name="username"/&gt;

      【讨论】:

        【解决方案4】:

        这不是 PHP 错误,而是 HTML 错误。

        POSTS 变量在 HTML 中使用 name 标记,而不是 id 标记。

        您的输入应如下所示:

        <input type="text" name="username"/>
        

        只需将id 标签更改为name 标签,它应该可以工作。

        【讨论】:

          【解决方案5】:

          更改您的 HTML 表单

           <?php require('scripts/validate.php');?>
              <!DOCTYPE html>
              <html>
              <head>
          
                <meta charset="UTF-8" />
                <title>Register</title>
          
              <body>
               <div id="mainWrapper">
                  <div id="register">
                      <?php if(isset($error)){echo "<div id='error'>".$error."</div>";}?>
                      <?php if(isset($success)){echo "<div id='success'>".$success."</div>";}?>
                    <form method="post" action="scripts/validate.php" >
                      <fieldset>
                          <legend>Register Here</legend>
                          <p>
                          <label for="Username">Username</label>
                          <input type="text" id="username" name="username"/>
                          </p>
                          <p>
                          <label for="Email">Email</label>
                          <input type="text" id="email" name="email"/>
                          <p>
                          <label for="Firstname">Firstname</label>
                          <input type="text" id="firstname" name="firstname"/>
                          </p>
                          <p>
                          <label for="Lastname">Lastname</label>
                          <input type="text" id="lastname" name="lastname"/>
                          </p>
                          <p>
                          <label for="Password">Password</label>
                          <input type="password" id="password" name="password"/>
                          </p>
                          <p>
                          <label for="Re-Type Password">Re-Type Password</label>
                          <input type="password" id="password2" name="password2"/>
                          </p>
                          <p>
                          <label for="DOB">DOB</label>
                          <input type="text" id="dob" name="dob">
                          </p>
                          <p>
                          <label for="Adress">Adress</label>
                          <input type="text" id="address" name="address"/>
                          </p>
                          <p>
                          <label for="Adress 2">Adress 2</label>
                          <input type="text" id="address2" name="address2"/>
                          </p>
                          <p>
                          <label for="town">Town</label>
                          <input type="text" id="town" name="town"/>
                          </p>
                          <p>
                          <label for="county">County</label>
                          <input type="text" id="county" name="county"/>
                          </p>
                          <p>
                          <label for="Postalcode">PostalCode</label>
                          <input type="text" id="postcode" name="postcode"/>
                          </p>
                          <p>
                          <label for="contactno">Contact No.</label>
                          <input type="text" id="contact" name="contact"/>
                          </p>
                        <input type="submit" name="submit" value="Register"/>
                          </fieldset>
                          </form>
                          </div>
                      </div>
              </body>
              </html>
          

          【讨论】:

            【解决方案6】:

            正如其他人在这里所说,您的表单元素需要一个名称属性。 id 属性非常适合引用 JavaScript 或 CSS 中的表单元素。但是对于 $_POST 变量,您需要指定名称。希望这能让您理解为什么需要 name 属性而不是 id 属性。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-08-29
              • 1970-01-01
              • 1970-01-01
              • 2014-12-27
              • 2015-05-17
              • 1970-01-01
              相关资源
              最近更新 更多