PHP - 注意:未定义索引:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13707448/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:55:20  来源:igfitidea点击:

PHP - Notice: Undefined index:

mysqlphp

提问by Brian

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

可能重复:
PHP:“注意:未定义变量”和“注意:未定义索引”

I am trying to do a registration form that registers a user in a database (MySQL).

我正在尝试做一个在数据库(MySQL)中注册用户的注册表。

The code is supposed to register:

该代码应该注册:

  • Name
  • Surname
  • Username
  • Password
  • Email
  • Security Question
  • Security Answer
  • 姓名
  • 用户名
  • 密码
  • 电子邮件
  • 安全问题
  • 安全答案

The code, before saving to the table, checks whether the username or password already exists, and if they do it echoes an error message, and if not, it saves to the db.

代码在保存到表之前,检查用户名或密码是否已经存在,如果存在,则回显错误消息,如果不存在,则保存到数据库中。

However, on page load, I'm getting these errors:

但是,在页面加载时,我收到以下错误:

Notice: Undefined index: Name in C:\xampp\htdocs\Task2\registration.php on line 216

Notice: Undefined index: Surname in C:\xampp\htdocs\Task2\registration.php on line 217

Notice: Undefined index: Username in C:\xampp\htdocs\Task2\registration.php on line 219

Notice: Undefined index: Email in C:\xampp\htdocs\Task2\registration.php on line 221

Notice: Undefined index: C_Email in C:\xampp\htdocs\Task2\registration.php on line 222

Notice: Undefined index: password in C:\xampp\htdocs\Task2\registration.php on line 224

Notice: Undefined index: c_password in C:\xampp\htdocs\Task2\registration.php on line 225

Notice: Undefined index: SecQ in C:\xampp\htdocs\Task2\registration.php on line 227

Notice: Undefined index: SecA in C:\xampp\htdocs\Task2\registration.php on line 228

Notice: Undefined variable: mysql_connect in C:\xampp\htdocs\Task2\registration.php on line 231

Fatal error: Function name must be a string in C:\xampp\htdocs\Task2\registration.php on line 231

This is the code:

这是代码:

    <?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

  <title>Registration</title>

  <style type="text/css" title="Amaya theme">

body {
    font-size: 12pt;
    font-family: Helvetica, Arial, sans-serif;
    font-weight: normal;
    font-style: normal;
    color: black;
    background-color: white;
    line-height: 1.2em;
    margin-left: 4em;
    margin-right: 2em;
   }

/* paragraphs */
p  {
    padding: 0;
    margin-top: 1em;
    margin-bottom: 1em;
    text-align: left;
   }

/* headings */
h1 {
    font-size: 180%;
    font-weight: bold;
    font-style: normal;
    font-variant: small-caps;
    text-align: left;
    padding: 0;
    margin-top: 1.7em;
    margin-bottom: 1.7em;
   }
h2 {
    font-size: 150%;
    font-weight: bold;
    font-style: normal;
    padding: 0;
    margin-top: 1.5em;
    margin-bottom: 1.1em;
   }
h3 {
    font-size: 130%;
    font-weight: bold;
    font-style: normal;
    padding: 0;
    margin-top: 1.3em;
    margin-bottom: 1.1em;
   }
h4 {
    font-size: 110%;
    font-weight: bold;
    font-style: normal;
    padding: 0;
    margin-top: 1.1em;
    margin-bottom: 1.1em;
   }
h5 {
    font-size: 100%;
    font-weight: bold;
    font-style: italic;
    padding: 0;
    margin-top: 1em;
    margin-bottom: 1em;
   }
h6 {
    font-size: 100%;
    font-weight: normal;
    font-style: italic;
    padding: 0;
    margin-top: 1em;
    margin-bottom: 1em;
   }

/* divisions */
div {
    padding: 0;
    margin-top: 0em;
    margin-bottom: 0em;
   }

/* lists */
ul, ol {
    padding: 0 0 0 3em;
    margin-top: 1em;
    margin-bottom: 1em;
   }
ul ul, ol ol, ul ol, ol ul {
    margin-top: 1em;
    margin-bottom: 1em;
   }
li {
    padding: 0;
    margin-top: 1em;
    margin-bottom: 1em;
    text-align: left;
   }
li p {
    margin-top: 1em;
    margin-bottom: 1em;
   }
dl {
    padding: 0;
    margin-top: 1em;
    margin-bottom: 1em;
    margin-left: 1em;
   }
dl dl {
    margin-top: 0em;
    margin-bottom: 0em;
   }
dt {
    padding: 0;
    font-weight: bold;
    margin-top: .3em;
    margin-bottom: .3em;
   }
dd {
    padding: 0;
    margin-top: .3em;
    margin-left: 3em;
    margin-bottom: .3em;
   }
dl p {
    margin-top: .3em;
    margin-bottom: .3em;
   }

/* inline */
strong {
    font-weight: bold;
   }
em {
    font-style: italic;
   }
code {
    font-family: Courier New, Courier, monospace;
   }
ins {
    background-color: yellow;
    text-decoration: underline;
   }
del {
    text-decoration: line-through;
   }


a[href] {
    color: blue;
    text-decoration: underline;
   }


  </style>
</head>

<body>
<h4><span style="font-family: Arial,Helvetica,sans-serif">Name</span></h4>

<p style="font-family: Arial,Helvetica,sans-serif"> <input type="text"  name='Name'/></p>

<h4 style="font-family: Arial,Helvetica,sans-serif">Surname</h4>

<p style="font-family: Arial,Helvetica,sans-serif"> <input type="text" name='Surname'/></p>

<h4 style="font-family: Arial,Helvetica,sans-serif">Username</h4>

<p style="font-family: Arial,Helvetica,sans-serif"> <input type="text" name='Username' /></p>
<hr style="font-family: Arial,Helvetica,sans-serif" />

<h4 style="font-family: Arial,Helvetica,sans-serif">Email</h4>

<p style="font-family: Arial,Helvetica,sans-serif"> <input type="text" name='Email' /></p>

<h4 style="font-family: Arial,Helvetica,sans-serif">Confirm Email</h4>

<p style="font-family: Arial,Helvetica,sans-serif"> <input type="text" name='C_Email' /></p>
<hr style="font-family: Arial,Helvetica,sans-serif" />

<h4 style="font-family: Arial,Helvetica,sans-serif">Password</h4>

<p style="font-family: Arial,Helvetica,sans-serif"> <input type="password" name='password' /></p>

<h4 style="font-family: Arial,Helvetica,sans-serif">Confirm Password</h4>

<p style="font-family: Arial,Helvetica,sans-serif"> <input type="password" name='c_password'/></p>
<hr style="font-family: Arial,Helvetica,sans-serif" />

<h4 style="font-family: Arial,Helvetica,sans-serif">Security Question</h4>

<p style="font-family: Arial,Helvetica,sans-serif"> <input type="password" name='SecQ' /></p>

<h4 style="font-family: Arial,Helvetica,sans-serif">Security Answer</h4>

<p style="font-family: Arial,Helvetica,sans-serif"> <input type="password" name='SecA'/></p>
<hr style="font-family: Arial,Helvetica,sans-serif" />

<hr style="font-family: Arial,Helvetica,sans-serif" />


<p><span
style="font-family: Arial,Helvetica,sans-serif"><button>Submit</button></span></p>

</body>

</html>
<?php
$Name = $_POST['Name'];
$Surname = $_POST['Surname'];

$Username = $_POST['Username'];

$Email = $_POST['Email'];
$C_Email = $_POST['C_Email'];

$Password = $_POST['password'];
$C_Password = $_POST['c_password'];

$SecQ = $_POST['SecQ'];
$SecA = $_POST['SecA'];


$mysql_connect ('localhost', 'admin', 'storefile1234');
mysql_select_db ("storefile");

$check_username = mysql_query("SELECT FROM users WHERE username = '$Username'");
$check_email = mysql_query("SELECT FROM users WHERE email = '$Email'");


if ($Name == null || $Surname== null || $Username == null || $Password == null || $C_Password == null || $Email == null || $C_Email == null || $SecQ == null || $SecA == null ) {

    echo "Missing details. Please enter all fields.";


} else {

    if(mysql_num_rows($check_username) != 0 && mysql_num_rows($check_email) != 0)
            {
            echo "Username/Email already exists";
            }
            if  ($Email == $C_Email && $Password == $C_Password) {

                $query = "INSERT INTO users (Username, Name,Surname, Password, Email, SecQ, SecA) VALUES ('NULL', ".$Username."', ".$Name."', ".$Surname."', ".$Password."', ".$SecQ."', ".$SecA."', ".$Email.')"';

                mysql_query($query) or die ('Error registering.');

                echo "Greetings, ".$Name.", you have been registered. ";

    }  else {

        echo "Error registering your account. Please try again.";
            }

 }


?>

Any help would be appreciated.

任何帮助,将不胜感激。

Thanks!

谢谢!

回答by Ryan

Before you extract values from $_POST, you should check if they exist. You could use the issetfunction for this (http://php.net/manual/en/function.isset.php)

在从 中提取值之前$_POST,您应该检查它们是否存在。您可以isset为此使用该功能(http://php.net/manual/en/function.isset.php)

回答by Jeremy C

You're getting errors because you're attempting to read post variables that haven't been set, they only get set on form submission. Wrap your php code at the bottom in an

您收到错误是因为您试图读取尚未设置的帖子变量,它们仅在表单提交时设置。将您的 php 代码包裹在底部

if ($_SERVER['REQUEST_METHOD'] === 'POST') { ... }

if ($_SERVER['REQUEST_METHOD'] === 'POST') { ... }

Also, your code is ripe for SQL injection. At the very least use mysql_real_escape_stringon the post vars before using them in SQL queries. mysql_real_escape_stringis not good enough for a production site, but should score you extra points in class.

此外,您的代码对于 SQL 注入已经成熟。在mysql_real_escape_stringSQL 查询中使用它们之前,至少在 post vars 上使用它们。mysql_real_escape_string对于生产站点来说还不够好,但应该在课堂上给你加分。

回答by mrq

How are you loading this page? Is it getting anything on POST to load? If it's not, then the $name = $_POST['Name']; assignation doesn't have any 'Name' on POST.

你是如何加载这个页面的?是否在 POST 上加载任何内容?如果不是,则 $name = $_POST['Name']; 分配在 POST 上没有任何“名称”。

回答by Darius

For starters,

对于初学者,

mysql_connect() should not have a $ accompanying it; it is not a variable, it is a predefined function. Remove the $ to properly connect to the database.

mysql_connect() 不应该有 $ 伴随它;它不是一个变量,它是一个预定义的函数。删除 $ 以正确连接到数据库。

Why do you have an XML tag at the top of this document? This is HTML/PHP - a HTML doctype should suffice.

为什么在本文档的顶部有一个 XML 标记?这是 HTML/PHP - HTML 文档类型就足够了。

From line 215, update:

从第 215 行开始,更新:

if (isset($_POST)) {
    $Name = $_POST['Name'];
    $Surname = $_POST['Surname'];

    $Username = $_POST['Username'];

    $Email = $_POST['Email'];
    $C_Email = $_POST['C_Email'];

    $Password = $_POST['password'];
    $C_Password = $_POST['c_password'];

    $SecQ = $_POST['SecQ'];
    $SecA = $_POST['SecA'];
}

POST variables are coming from your form, and you have to check whether they exist or not, else PHP will give you a NOTICE error. You can disable these notices by placing error_reporting(0); at the top of your document. It's best to keep these visible for development purposes.

POST 变量来自您的表单,您必须检查它们是否存在,否则 PHP 会给您一个 NOTICE 错误。您可以通过放置 error_reporting(0) 来禁用这些通知;在文档的顶部。为了开发目的,最好保持这些可见。

You should only be interacting with the database (inserting, checking) under the condition that the form has been submitted. If you do not, PHP will run all of these operations without any input from the user. Its best to use an IF statement, like so:

您应该只在表单已提交的情况下与数据库交互(插入、检查)。如果不这样做,PHP 将在没有用户输入的情况下运行所有​​这些操作。最好使用 IF 语句,如下所示:

if (isset($_POST['submit']) {
// blah blah
// check if user exists, check if fields are blank
// insert the user if all of this stuff checks out..
} else {
// just display the form
}

Awesome form tutorial: http://php.about.com/od/learnphp/ss/php_forms.htm

很棒的表单教程:http: //php.about.com/od/learnphp/ss/php_forms.htm