我的PHP代码有错误

时间:2017-04-12 23:14:50

标签: php

我发现错误但代码正常运行! 这是我的代码

 <?php

 require('connect.php');
 $name = $_POST['name'];
 $comment = $_POST['comment'];
 $submit = $_POST['submit'];

 if ($submit) {
     if ($name && $comment) {
         $insert = mysql_query("INSERT INTO comment (name, comment) VALUES ('$name', '$comment')");
     } else {
         echo "Please fill all fields";
     }
 }
 ?>
 <!DOCTYPE html>
 <html>
 <head>
 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 </head>
 <body>
 <form action="index.php" method="POST">
 <table>
 <tr><td>Name: </td><td><input type="text" name="name" /></td></tr>
 <tr><td colspan="2">Comment: </td></tr>
 <tr><td colspan="2"><textarea name="comment"></textarea></td></tr>
 <tr><td colspan="2"><input type="submit" name"submit" value="Comment"/>      </td></tr>
 </table>
 </form>

 <?php
 $getquery = mysql_query("SELECT * FROM comment ORDER BY id DESC");
 while($rows = mysql_fetch_assoc($getquery))
 {
     $id = $rows['id'];
     $name = $rows['name'];
     $comment = $rows['comment'];

     echo $name . "<br />" . $comment ;
 }
 ?>
 </body>

这是我得到的错误:

  

注意:未定义的索引:第4行的C:\ xampp \ htdocs \ index.php中的名称
  注意:未定义的索引:第5行的C:\ xampp \ htdocs \ index.php中的注释   注意:未定义的索引:在第6行的C:\ xampp \ htdocs \ index.php中提交

但我再说一遍,无论错误如何,代码都能正常运行!错误始终存在......

1 个答案:

答案 0 :(得分:0)

在第一次访问时,可能没有$ _POST

$name = isset($_POST['name']) ? $_POST['name'] : '';
$comment = isset($_POST['comment']) ? $_POST['comment'] : '';
$submit = isset($_POST['submit']) ? $_POST['submit'] : '';