PHP查询两次插入数据?

时间:2014-07-02 11:34:35

标签: php mysql arrays insert fetch

我正在尝试为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();
?>

这个软件将是开源的,所以你真的通过帮助来为这个功能做出贡献,因为我之前从未使用过这个功能。

谢谢!

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

    <?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();
            ?>