【问题标题】:The Data doesn't appear in the Database when inserted from php&code从 php&code 插入时,数据不会出现在数据库中
【发布时间】:2014-06-24 08:56:55
【问题描述】:

我的代码

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

这是非常简单的代码,当我打开文件时出现以下错误:

但是在我在 textarea 中添加一些东西后它们消失了,但是插入的数据没有进入数据库。

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

请帮助我尝试了所有方法。

【问题讨论】:

  • a) 该代码本身易受 SQL 注入攻击 b) 您实际上是在 connect.php 中打开连接还是仅包含必要的类/函数?
  • 另外:您当前分别在输入和文本区域输入什么文本来测试代码?

标签: php mysql database comments textarea


【解决方案1】:

改变

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

【讨论】:

  • 谢谢,但我遇到了同样的错误。注意到改变了。
  • 我尝试添加这个 if (!empty($_POST)) 提交错误消失了,但其余的仍然存在并注意到数据库中的显示
  • 更新了我的答案..我认为您的 sql 中有一些错误..尝试回显 sql 并执行它以查找错误。
  • 是的,有一个小错误,根据您的想法,我摆脱了提交错误,谢谢。
【解决方案2】:

不要使用$var = $_POST['sth']。使用这个:

$sth = null;
if(isset($_POST['sth'])) {
    $sth = $_POST['sth'];
}

或者,更短的方式:

$sth = isset($_POST['sth']) ? $_POST['sth'] : null;

您也可以使用'' 或false 代替null

替换这个:

$name=$_POST['name'];
$comment=$_POST['comment'];
$submit=$_POST['submit'];

用这个:

$name    = issset($_POST['name'])   ? addslashes($_POST['name'])    : false;
$comment = isset($_POST['comment']) ? addslashes($_POST['comment']) : false;
$submit  = isset($_POST['submit'])  ? addslashes($_POST['submit'])  : false;

要检查是否存在mysql错误,请替换此行:

$insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment')"); 

用这个:

$insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment')") or die(mysql_error()); 

【讨论】:

  • 所有错误都消失了我也使用了这个代码
  • if (!empty($_POST)) 和你建议的那个 $comment=null; if(isset($_POST['comment'])) { $comment = $_POST['comment']; }
  • 但是数据没有出现在数据库中。
  • 我更新了我的答案。如果结果相同,则在查询后添加if(!$insert) die(mysql_error()) else echo 'The data has been successfully insertted';
【解决方案3】:
<?php

 require('connect.php');
 if(isset($_POST)){    //use condition for post
 $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>

【讨论】:

    【解决方案4】:

    您可以将代码重写为,添加 isset() 。

        if(isset($_POST['submit'])){
            $name= $_POST['name'];
            $comment= $_POST['comment'];
            if(!empty($name) && !empty($comment)){
                $insert=mysql_query("INSERT INTO `comment` (`name`,`comment`) VALUES ('$name','$comment')") or die(mysql_error());
            }else{ 
                echo" please fill out the fields";
            }
    
        }
    

    注意:使用 mysqli_* 或 PDO 函数而不是使用 mysql_* 函数(已弃用)

    【讨论】:

      【解决方案5】:

      您应该检查是否已发布表单 在&lt;?PHP 之后使用if($_POST),在?&gt; 之前使用}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-04
        • 1970-01-01
        • 2020-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-01
        • 1970-01-01
        相关资源
        最近更新 更多