我的变量未定义?

时间:2015-07-10 17:55:23

标签: php

我有一个名为blog.php的文件。 Db名称为blog,表名为comments。 (这是一个评论框)

connect.php是我与服务器的连接。 actionblog.php

我的未定义变量在线:3,4& 5。 我不知道为什么它们是未定义的,因为在教程中没有错误。你能告诉我他们为什么不明确吗?

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

 if($submit){

   if($name && $comment){

     $insert = mysql_query("INSERT INTO blog(name,comment) VALUES('$name',' $comment')");

   }else{
     echo "Please fill out all the fields.";
   }

 }
 ?>


<!DOCTYPE html>
<html>
 <head></head>
 <body>
 <form action="blog.php" method ="POST">
  <table border="2" style="width: 250px; ">
    <th> Post A Comment: </th>
    <tr><td colspan="2">Name: <input type="text" name="name"></td></tr>
    <tr><td >Comment: <textarea style="height: 100px; width: 200px;" name="comment"></textarea></td></tr>
  </table>
  <input type="submit" value="Comment" style="margin-left: 178px;" name="submit">
</form>
 </body
</html>

1 个答案:

答案 0 :(得分:3)

代码执行时会收到警告而不提交表单。

为避免这种情况,只需在if isset($_POST)请求完成后,即在提交表单后添加POST即可执行。

<?php
 require('connect.php');


if (isest($_POST)){
 $name = $_POST['name'];
 $comment = $_POST['comment'];
 $submit = $_POST['submit'];

 if($submit){

   if($name && $comment){

     $insert = mysql_query("INSERT INTO blog(name,comment) VALUES('$name',' $comment')");

   }else{
     echo "Please fill out all the fields.";
   }

 }
}
 ?>
相关问题