php - 从 php&code 插入时,数据没有出现在数据库中

标签 php mysql database comments textarea

我的代码

<?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/

相关文章:

mysql - #1064 mysql 错误 - DOUBLE NOT NULL

php - 如何避免记录由 Api-Platform 正确转换为状态代码的预期异常?

带有 Microsoft SQL Server 2008 R2 的 PHP 5.5.0 - 没有 sqlsrv_connect()?

php - 具有动态 ID 的 Bootstrap 折叠面板不起作用

php - 查询中的 ORDER BY 不适用于循环

php - 如何从 MySql 获取日期明智的报告

php - Mysql插入时出现重复条目​​错误

javascript - 第一次更新时对填充字段进行 Sequelize

php - 无法使用用户: Unknown database 'users'

mysql - 为什么当前的数据库查询优化技术不支持计算列的优化?