MYSQL Fetch Array Inserting Double Query

Fait

Seasoned Veteran
Joined
Oct 15, 2010
Messages
4,407
Reaction score
561
FP$
2,054
Hello,

I am trying to make a feature for the Mybb system going to call it social groups, so far it's been great but when I try to insert a comment in the database it inserts it twice due to the msql_fetch_array and how it works (php should fix this)

Anyway how can I get the Posts id the user is commenting on and insert it only once in the database not twice
Code:
<?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($mybb->input['post_message_submit'])) {
$post_message_submit = $mybb->input['post_message_submit'];
$post_message = $mybb->input['post_message'];
$comment_post = $mybb->input['comment_post'];
   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>____________";


$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_query2 = $db->query("INSERT INTO " . TABLE_PREFIX . "groups_comments" . "(comment_by, post_id, post_body)
VALUES ('$mybb_username', '$post_id_row' ,'$comment_post')");




echo "<br> 
<form action='' method='POST'>
<input type='text' name='comment_post' placeholder='Comment then Hit Enter'>
</form>
";




   
}

//We have done everything we need to do we can now exit and not execute anything beyond this point
exit();
?>
[/code]
This software is going to be open source so you are really contributing to the feature by helping as I have never gone this advanced before.



Thanks!
 
I don't understand why you have the same insert query at two places.

I just commented the second query, try and let me know.
Code:
<?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();
?>
 
Same Issue :/

The query you commented out was only for testing purposes. The original query was to insert the comment to a certain post/status in to the database but instead of inserting it once it's inserting how many loops the fetch_array is looping (say if the array finds 4 status updates/posts it will post the comment 4 times)
Something to do with it being in the fetch array but it needs to be there in order to get the posts id.

Thanks for the help so far and it is odd how its even doing this
 
Note: I have commented the INSERT query within the loop

Please be clear, which query meant for testing has to be commented?
the query at the top?
or the query within the loop?
 
The query that is with in the loop

That is probably why its inserting the data more then it should since its probably not a meant to be with in the fetch array, but need it there to get the post/status id.
 
if the query within the loop is commented out, then there is no way it can insert twice.
is that the full code?
 
I don't mean commented out lol, When its not commented out and functional on the site.

Could the fetch_array be doing it?

If so what would be another way to get the post id?


Just noticed the code uploaded does have it commented it out.
Thanks 😉<br /><br />-- July 3rd, 2014, 8:34 am --<br /><br />Might just use a plugin
 
Back
Top Bottom