mysql - PHP - 通知 : Undefined index:

标签 mysql php

<分区>

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

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

代码应该注册:

  • 姓名
  • 姓氏
  • 用户名
  • 密码
  • 电子邮件
  • 安全问题
  • 安全答案

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

但是,在页面加载时,我遇到了这些错误:

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

这是代码:

    <?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.";
            }

 }


?>

如有任何帮助,我们将不胜感激。

谢谢!

最佳答案

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

关于mysql - PHP - 通知 : Undefined index:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13707448/

相关文章:

php - 模型验证中的 Phalcon 可选字段

php - 如果提供的数据在 mySQL 中不为空,则更新字段

php - 向搜索表单添加错误

mysql - vb.net 消费nusoap报错

mysql - 将两个 60,000 行表连接在一起并获得 1000 万行?

php - 对于强大的、可扩展的开发平台,您会建议使用什么框架?

php - 使用免费图像托管是否是 CDN 的可行替代方案?

php - 覆盖 DBAL 类型

mysql - 从服务器接收到的字段的未知字符集索引

两个子表的 MySQL JOIN 问题