【问题标题】:PHP Query Inserting Data Twice?PHP查询插入数据两次?
【发布时间】:2014-08-23 04:22:24
【问题描述】:

我正在尝试为 Mybb 系统创建一个功能,将其称为社交组,到目前为止它一直很棒,但是当我尝试在数据库中插入评论时,由于 msql_fetch_array 及其工作原理,它会插入两次( php应该解决这个问题)

无论如何,我怎样才能获得用户正在评论的帖子 ID,并且只在数据库中插入一次而不是两次

<?php
require_once ("core/connection.php");
require_once ("core/group_functions.php");



    //We need to post the message update in to the database
if(isset($_POST['post_message_submit'])) {
$post_message_submit = $_POST['post_message_submit'];
$post_message = $_POST['post_message'];
    if(($post_message_submit) && ($post_message)) {

    $insert_query = $db->query("INSERT INTO " . TABLE_PREFIX . "groups_posts" . "(posted_by, group_name, post_body)
VALUES ('$mybb_username', '$get_group_url' ,'$post_message')");


    } else {
    echo "<text style='color:red;'> You Must Specify A Message</a></text>";
    }
    }



echo "
    <form action='' method='POST'>
<textarea name='post_message' id='post_message' placeholder='Whats Going On?'></textarea><br> 
       <input type='submit' name='post_message_submit' value='Post'>
        </form>

";


$fetch_index_query = $db->query("SELECT post_id,posted_by,post_body,post_active,group_name FROM " . TABLE_PREFIX . "groups_posts WHERE group_name='$get_group_url'");





while($fetch_index_groups_array = $db->fetch_array($fetch_index_query)) {
$post_id_row = $fetch_index_groups_array['post_id'];
$posted_by = $fetch_index_groups_array['posted_by'];
$g_name = $_fetch_index_groups_array['g_name'];
$g_body = $fetch_index_groups_array['post_body'];
echo("<br>" . "<a href=''> $posted_by </a>" . "<br>" . $gname
. "<br>____________
<br> 
<form action='' method='POST'>
<input type='text' name='comment_post' placeholder='Comment then Hit Enter'>
</form>
"
);
$fetch_comments_query = $db->query("SELECT g_name,post_body,comment_by FROM spud_groups_comments WHERE post_id='$post_id_row'");
while($fetch_groups_comments = $db->fetch_array($fetch_comments_query)) {
$post_body = $fetch_groups_comments['post_body'];




echo ("<br>" . $post_body);



}
$insert_query = $db->query("INSERT INTO " . TABLE_PREFIX . "groups_posts" . "(posted_by, group_name, post_body)
VALUES ('$mybb_username', '$get_group_url' ,'$post_message')");

}

//We have done everything we need to do we can now exit and not execute anything beyond this point
exit();
?>

该软件将是开源的,因此您可以通过提供帮助来真正为该功能做出贡献,因为我以前从未如此先进。

谢谢!

【问题讨论】:

  • $db 对象是什么?它是否只处理一个结果集?
  • 那是mybb论坛软件对象,它只是用db替换了MySQL
  • 您循环 $fetch_index_query 并为每个返回的行执行插入。插入中的任何内容都不依赖于 $fetch_index_query 中的任何内容,因此将插入放入循环中似乎没有意义。但是你的代码很容易受到 SQL 注入攻击。

标签: php mysql arrays insert fetch


【解决方案1】:

因为你使用了两次插入查询:)

【讨论】:

  • 我忘了提,哈哈,我确实将查询放入了两次以进行测试,但它确实有另一个查询来插入评论,但由于某种原因,fetch_array 将它从多少个 cmets 插入到数据库中它找到了(比如如果找到 4 个帖子,它将在数据库中插入评论 4 次)
  • 不要说 fetch_array 是在插入它,实际上你是在同一个页面上提交表单,并且表单提交后 php 脚本可能会再次运行。所以尝试为你的项目创建文件和模块
  • 我可以尝试,但我已将插入查询从 fetch_array 中取出,它只插入一次,但我需要在 fetch 数组中获取 row_id
  • 一个好建议:永远不要创建动态表单。不要试图在 php 中包含 HTML。这实际上是一种不好的做法。 html和php尽量分开使用。
  • 我只是出于测试目的这样做,但我很快就会将它们分开
【解决方案2】:
    <?php
            require_once ("core/connection.php");
            require_once ("core/group_functions.php");



                //We need to post the message update in to the database
            if(isset($_POST['post_message_submit'])) {
            $post_message_submit = $_POST['post_message_submit'];
            $post_message = $_POST['post_message'];
                if(($post_message_submit) && ($post_message)) {

               // $insert_query = $db->query("INSERT INTO " . TABLE_PREFIX . "groups_posts" . "(posted_by, group_name, post_body) VALUES ('$mybb_username', '$get_group_url' ,'$post_message')");


                } else {
                echo "<text style='color:red;'> You Must Specify A Message</a></text>";
                }
                }



            echo "
                <form action='' method='POST'>
            <textarea name='post_message' id='post_message' placeholder='Whats Going On?'></textarea><br> 
                   <input type='submit' name='post_message_submit' value='Post'>
                    </form>

            ";


            $fetch_index_query = $db->query("SELECT post_id,posted_by,post_body,post_active,group_name FROM " . TABLE_PREFIX . "groups_posts WHERE group_name='$get_group_url'");





            while($fetch_index_groups_array = $db->fetch_array($fetch_index_query)) {
            $post_id_row = $fetch_index_groups_array['post_id'];
            $posted_by = $fetch_index_groups_array['posted_by'];
            $g_name = $_fetch_index_groups_array['g_name'];
            $g_body = $fetch_index_groups_array['post_body'];
            echo("<br>" . "<a href=''> $posted_by </a>" . "<br>" . $gname
            . "<br>____________
            <br> 
            <form action='' method='POST'>
            <input type='text' name='comment_post' placeholder='Comment then Hit Enter'>
            </form>
            "
            );
            $fetch_comments_query = $db->query("SELECT g_name,post_body,comment_by FROM spud_groups_comments WHERE post_id='$post_id_row'");
            while($fetch_groups_comments = $db->fetch_array($fetch_comments_query)) {
            $post_body = $fetch_groups_comments['post_body'];




            echo ("<br>" . $post_body);



            }
            $insert_query = $db->query("INSERT INTO " . TABLE_PREFIX . "groups_posts" . "(posted_by, group_name, post_body)
            VALUES ('$mybb_username', '$get_group_url' ,'$post_message')");

            }

            //We have done everything we need to do we can now exit and not execute anything beyond this point
            exit();
            ?>

【讨论】:

  • 我忘了提,哈哈,我确实将查询放入了两次以进行测试,但它确实有另一个查询来插入评论,但由于某种原因,fetch_array 将它从多少个 cmets 插入到数据库中找到了(比如说如果找到 4 个帖子,它将在数据库中插入评论 4 次)感谢您到目前为止的尝试;)
【解决方案3】:

从代码中删除第二个插入查询。

【讨论】:

    猜你喜欢
    • 2017-07-31
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 2020-12-25
    • 2014-07-21
    • 2016-01-25
    • 2023-03-04
    • 2015-06-05
    相关资源
    最近更新 更多