我有一个名为 blog.php
的文件。
数据库名称是 blog
表名是comments
。 (这是评论框)
connect.php
是我与服务器的连接。 action
是 blog.php
.
我的 undefined variable 位于第 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>
最佳答案
当代码在未提交表单的情况下执行时,您会收到警告。
为了避免这种情况,请添加一个 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.";
}
}
}
?>
关于php - 我的变量未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31347284/