表单向mysql表提交重复的注释

时间:2015-04-26 13:10:45

标签: php mysql

我在我的页面上做了一个注释部分,它将注释存储在mysql数据库中。但是,当我单击“发送”时,它会将相同注释的副本发送到数据库中。我无法看到我在哪里发送了两次查询。

<?php
            if (!empty($_POST) && !empty($_POST['name']) && !empty($_POST['message'])) { 
                $name = strip_tags($_POST['name']);
                $message = strip_tags($_POST['message']);
                $name = mysqli_real_escape_string($db, $name);
                $message = mysqli_real_escape_string($db, $message);

                $query = "SELECT COUNT(*) AS count FROM comments";
                 $result = $db->query($query);
                 $data = $result->fetch_assoc();


                 $query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
                 $db->query($query);
                 $result = $db->query($query);

                 $query = "INSERT INTO `comment_work` (`comment_id`,`work_id`) SELECT `comments`.`id`, `comments`.`work_id` FROM `comments` WHERE `comments`.`sender_ name` = '".$name."' AND `comments`.`message` = '".$message."'";
                 $db->query($query);
                    $result = $db->query($query);




            if ($result) {
              echo "<p>Comment submitted!</p>";
            } else {
                echo "SQL Error: " . $db->error;
            }
            }

        ?>

2 个答案:

答案 0 :(得分:1)

这里

$query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
$db->query($query);//one query - **YOU must remove this line!**
$result = $db->query($query);//second query

$query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
$result = $db->query($query);

在这里

$query = "INSERT INTO `comment_work` (`comment_id`,`work_id`) SELECT `comments`.`id`, `comments`.`work_id` FROM `comments` WHERE `comments`.`sender_ name` = '".$name."' AND `comments`.`message` = '".$message."'";
 $result = $db->query($query);

答案 1 :(得分:1)

原因很简单:

查看以下代码行:

 $query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
                 $db->query($query);
                 $result = $db->query($query);

                 $query = "INSERT INTO `comment_work` (`comment_id`,`work_id`) SELECT `comments`.`id`, `comments`.`work_id` FROM `comments` WHERE `comments`.`sender_ name` = '".$name."' AND `comments`.`message` = '".$message."'";
                 $db->query($query);
                    $result = $db->query($query);

好吧,在这里,您正在使用$db->query执行查询,然后使用$result = $db->query执行另一次同一查询。

替换它:

$query = "INSERT INTO `comment_work` (`comment_id`,`work_id`) SELECT `comments`.`id`, `comments`.`work_id` FROM `comments` WHERE `comments`.`sender_ name` = '".$name."' AND `comments`.`message` = '".$message."'";
$db->query($query);
$result = $db->query($query);

使用:

$query = "INSERT INTO `comment_work` (`comment_id`,`work_id`) SELECT `comments`.`id`, `comments`.`work_id` FROM `comments` WHERE `comments`.`sender_ name` = '".$name."' AND `comments`.`message` = '".$message."'";
$result = $db->query($query);

而且:

$query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
$db->query($query);
$result = $db->query($query);

使用:

$query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
$result = $db->query($query);

所以应该工作。