注意:未定义索引:用户名在 C:\xampp\htdocs\Registration\scripts\validate.php 第 6 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24139438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Notice: Undefined index: username in C:\xampp\htdocs\Registration\scripts\validate.php on line 6
提问by user3725685
I am trying to register a new user by posting their form data to the database via a php scriptregister.php
but I am getting an array of errors when I hit register, the data is supposed to be validated by a second script called validate.php
. My register.php
is shown below. Same errors exist when the form is empty and when is filled.
我正在尝试通过 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>
And the validate.php is here
而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");
}
}
}
?>
I'll greatly appreciate guys.
我将不胜感激。
回答by Albzi
Give your input
s a name
property.
给你的input
saname
财产。
E.g:
例如:
<input type="text" id="username" name="username" />
The issue is that it's looking for name
, but it doesn't exist.
问题是它正在寻找name
,但它不存在。
This goes for all of your input
fields, not just that specific one and not just for type="text"
.
这适用于您的所有input
领域,而不仅仅是那个特定领域,而不仅仅是type="text"
.
回答by edcs
When you POST data using a form, you need to specify the name of the data using the name
attribute. Replace all of the id
attributes with name
and your form will work. For example:
当您使用表单 POST 数据时,您需要使用name
属性指定数据的名称。用 替换所有id
属性,name
您的表单将起作用。例如:
<input type="text" id="username"/>
<input type="text" id="username"/>
should become:
应该变成:
<input type="text" name="username"/>
<input type="text" name="username"/>
回答by v2solutions.com
Replace all elements' id with name. When form is submitted the element's information is submitted with associated name and not id.
e.g. <input type="text" id="username" name="username"/>
用名称替换所有元素的 id。当表单被提交时,元素的信息是用关联的名称而不是 id 提交的。例如<input type="text" id="username" name="username"/>
回答by Scarraban
This isn't a PHP error, it's a HTML one.
这不是 PHP 错误,而是 HTML 错误。
POSTS variables are stated using the name
tag in HTML, not the id
tag.
POSTS 变量使用name
HTML 中的标签来表示,而不是id
标签。
Your inputs should look like this:
您的输入应如下所示:
<input type="text" name="username"/>
Just change the id
tags to name
tags, and it should work.
只需将id
标签更改为name
标签,它应该可以工作。
回答by Sathya Baman
Change your HTML Form
更改您的 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>
回答by jfh6
as others have stated here, your form elements need a name attribute. the id attribute is great for referencing the form elements in JavaScript or CSS. but with $_POST variables you need to specify the name. Hope this gives an understanding to why you need the name attribute instead of the id attribute.
正如其他人在此处所述,您的表单元素需要一个 name 属性。id 属性非常适合在 JavaScript 或 CSS 中引用表单元素。但是对于 $_POST 变量,您需要指定名称。希望这能让您理解为什么需要 name 属性而不是 id 属性。