我收到错误,但代码正常工作!这是我的代码
<?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>
这是我得到的错误:
注意:未定义的索引:C:\xampp\htdocs\index.php 第 4
行中的名称 注意:未定义的索引:注释在 C:\xampp\htdocs\index.php 第 5
行 注意:未定义的索引:在第 6 行的 C:\xampp\htdocs\index.php 中提交
但我再说一遍,无论错误如何,代码都可以工作! 错误一直在那里...
在第一次访问中,可能没有 $_POST
$name = isset($_POST['name']) ? $_POST['name'] : '';
$comment = isset($_POST['comment']) ? $_POST['comment'] : '';
$submit = isset($_POST['submit']) ? $_POST['submit'] : '';