我的代码

<?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 out the fields";
    }
}
?>

<html>
    <head>
        <title>
            The comment box
        </title>
    </head>

    <body>
        <form action="mainpage.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>
    </body>
</html>

这是非常简单的代码,当我打开文件时,我会得到以下错误:
但是当我在文本区域中添加一些内容后,它们就会消失,但是插入的数据不会进入数据库。
Notice: Undefined index: name in C:\xampp\htdocs\test\mainpage.php on line 5

Notice: Undefined index: comment in C:\xampp\htdocs\test\mainpage.php on line 6

Notice: Undefined index: submit in C:\xampp\htdocs\test\mainpage.php on line 7

请帮忙,我什么都试过了。

最佳答案

改变

<form action="mainpage.php" method="POST">


<form action="mainpage.php" method="post">

可能的(不区分大小写)值为get(默认值)和post
而不是
 $submit=$_POST['submit'];

 if($submit){

,尝试使用if (!empty($_POST))
在试图访问通过表单发布的变量之前,必须检查是否使用isset()设置了该变量。因为,在第一次访问页面而不提交表单时,它将抛出错误,因为值没有被发布。
将代码更改为:
<?php
require('connect.php');
if(!empty($_POST))
{
if(isset($_POST['name']))
   $name=$_POST['name'];
if(isset($_POST['comment']))
$comment=$_POST['comment'];
            $insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment')");

}
else {
        echo" please fill out the fields";
    }
?>

10-01 20:46
查看更多