可以在获取数组中使用插入查询

时间:2014-07-03 02:38:59

标签: php mysql sql fetch

我不知道为什么这个还没有被回答但我不知道,我想知道是否可以在我试过的while循环中添加插入查询, 但它会继续插入评论,然后它应该(如果它找到4个状态更新,它将在数据库中发布评论4次)

我知道我有两次插入查询这不是问题,因为我有一个查询,它向数据库提交注释,当前查询用于测试目的。

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

1 个答案:

答案 0 :(得分:0)

尝试为插入查询实例化其他$ DB对象。即,不要使用您用于获取数组的相同内容,因为下一次使用将覆盖您循环的第一个查询的结果。