我的代码
<?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";
}
?>
关于php - 从 php&code 插入时,数据没有出现在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23516825/