注意:第6行C:\xampp\htdocs\Registration\scripts\validate.php中的未定义索引:username

注意:第6行C:\xampp\htdocs\Registration\scripts\validate.php中的未定义索引:username,php,post,Php,Post,我试图通过php scriptregister.php将表单数据发布到数据库来注册一个新用户,但是当我点击register时,我得到了一个错误数组,数据应该由另一个名为validate.php的脚本进行验证。我的register.php如下所示。表单为空和填写时也存在相同的错误 <?php require('scripts/validate.php');?> <!DOCTYPE html> <html> <head> <meta cha

我试图通过php scriptregister.php将表单数据发布到数据库来注册一个新用户,但是当我点击register时,我得到了一个错误数组,数据应该由另一个名为validate.php的脚本进行验证。我的register.php如下所示。表单为空和填写时也存在相同的错误

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

  <meta charset="UTF-8" />
  <title>Register</title>
这里有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");   
        }
       }
}
?>
我将非常感谢各位。

为您的输入命名属性

例如:

问题是它在寻找名字,但它不存在


这适用于所有输入字段,不仅适用于特定字段,也适用于type=text。

使用表单发布数据时,需要使用name属性指定数据的名称。用name替换所有id属性,表单将正常工作。例如:

应成为:


用名称替换所有元素的id。提交表单时,元素的信息以关联名称而不是id提交。
e、 这不是PHP错误,而是HTML错误

POSTS变量是使用HTML中的名称标记而不是id标记声明的

您的输入应该如下所示:

<input type="text" name="username"/>
只需将id标记更改为name标记,它就可以工作。

更改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>

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

没有名称,没有值……谢谢大家,我消除了这些错误。但我还是有个问题,表单没有添加到数据库中,我从connection scriptconnection.php收到此错误。不推荐使用:mysql\u connect:mysql扩展不推荐使用,将来将被删除:在第7行的C:\xampp\htdocs\Registration\scripts\connection.php中使用mysqli或PDO。这里是连接脚本
<input type="text" name="username"/>
 <?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>