php中未定义的索引和变量

时间:2011-11-18 03:20:54

标签: php

这是一本教科书问题,其中我遵循了确切的编码。然而,我不断得到未定义的索引和未定义的vairables的错误。我继续检查我的代码,我认为我错过了疲劳的错误。这是代码。有什么建议。我逆流而上。这是htm file associated with this

以下是错误消息:

 Undefined index: firstname in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 10   PHP Notice: Undefined index: lastname in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on   line 11 PHP Notice: Undefined index: whenithappened in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 12 PHP Notice: Undefined index: howlong in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 13 PHP Notice: Undefined index: howmany in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 14 PHP Notice: Undefined index: aliendescription in  D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 15 PHP Notice: Undefined index: whattheydid in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 16 PHP Notice: Undefined index: fangspotted in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 17 PHP Notice: Undefined index: email in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 18 PHP Notice: Undefined index: other in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 19 PHP Notice: Undefined variable: name in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 33 

 <!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>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title>Alien Abduction2</title>
 </head>

 <body>
 <?php
 $first_name = $_POST['firstname'];
 $last_name = $_POST['lastname'];
 $when_it_happened = $_POST['whenithappened'];
 $how_long = $_POST['howlong'];
 $how_many = $_POST['howmany'];
 $alien_description = $_POST['aliendescription'];
 $what_they_did = $_POST['whattheydid'];
 $fang_spotted = $_POST['fangspotted'];
 $email = $_POST['email'];
 $other = $_POST['other'];

 $dbc = mysqli_connect('localhost','cis54student','student','cis54')
 or die('Error connecting to MySQL server');
 $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
"how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

 $result = mysqli_query($dbc, $query)
 or die('Error querying database.' . mysqul_error());

 mysqli_close($dbc);

 echo "Thanks for submitting the form $name<br />";
 echo "You were abducted '  $when_it_happened<br />";
 echo "And were gone for ' . $how_long <br />";
 echo "Number of aliens: ' . $how_many <br />";
 echo "Describe them: ' . $alien_description <br />";
 echo "The aliens did this:  $what_they_did <br />";
 echo "Was Fang there?  $fang_spotted <br />";
 echo "Other comments: ' . $other <br />";
 echo 'Your email address is ' . $email;


?>
</body>
</html>

4 个答案:

答案 0 :(得分:1)

我相信您收到通知是因为当您加载页面时(当它未提交时,即只需单击here),这些变量就没有定义。你有两个解决方案。

  1. 检查$_POST中是否存在提交按钮,然后采取相应措施
  2. 在使用之前,更改所有变量以使用$_POSTisset()数组进行测试。
  3. 解决方案#1:

    if( isset( $_POST['submit']))
    {
        $first_name = $_POST['firstname'];
        $last_name = $_POST['lastname'];
        $when_it_happened = $_POST['whenithappened'];
        ....
    }
    

    解决方案#2:

    $first_name = isset( $_POST['firstname']) ? $_POST['firstname'] : '';
    $last_name = isset( $_POST['lastname']) ? $_POST['lastname'] : '';
    ...
    

    此外,正如马里奥指出的那样,您mysql_error拼错了mysqul_error

答案 1 :(得分:1)

单独更改

<input id="fangspotted" 
       name="fangspotted" 
       type="radio" 
       value="yes" 
       checked="checked" />

没有警告:)

答案 2 :(得分:0)

看看你提交的地方。我将form action =“report.php”更改为creport.php,它解决了很大一部分问题。行中还有错误

echo "Thanks for submitting the form $name<br />";

因为$ name永远不会被定义。

我可以建议的一些改进

首先我建议看http://php.net/manual/en/function.extract.php。它将节省大量代码来提取$ _POST。

添加isset测试以确保在使用变量之前设置变量

在这种情况下你可以使用。

echo (isset($var)?$var:'');

如前所述,您的代码确实存在一些安全漏洞,并且应该在服务器端执行额外级别的错误检查(如果禁用了javascript)并且内联javascript不被视为良好实践(为表单提交添加事件监听器) 。你的老师可能还没有涵盖这些(或者可能甚至不知道更好)。

答案 3 :(得分:0)

你真正需要的是检查是否有POST请求。

上述答案中的解决方案#2没有多大意义 它的唯一目的是关闭错误信息 虽然只是在没有任何处理的情况下呕吐错误,但是只会让你的代码变得混乱。

所以,你必须检查是否有帖子请求。

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    //here goes all your code
}

您将看到没有错误消息。

真正需要的唯一情况是复选框类型。 所有其他类型总是被发送,因此无需检查它们。