0

I am geting error but code is working! this is my code

 <?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>

and this is the error that I get:

Notice: Undefined index: name in C:\xampp\htdocs\index.php on line 4
Notice: Undefined index: comment in C:\xampp\htdocs\index.php on line 5
Notice: Undefined index: submit in C:\xampp\htdocs\index.php on line 6

but I say again, the code works regardless of the error! error sits there all the time...

borracciaBlu
  • 4,017
  • 3
  • 33
  • 41

1 Answers1

0

In the first access, there may be not have the $_POST

$name = isset($_POST['name']) ? $_POST['name'] : '';
$comment = isset($_POST['comment']) ? $_POST['comment'] : '';
$submit = isset($_POST['submit']) ? $_POST['submit'] : '';
Jefferson Costa
  • 188
  • 1
  • 9