1

我在这里有一个发布系统,其中一个人单击“|评论”,然后出现一个 div,其中包含以下代码。

   <div class="commentbox" id="commentbox<?php echo $_row["id"];?>" style="display:none;"><div style="padding-right:10px;" align="right"><a class="close" href="#" id="<?php echo $_row['id'];?>">X</a></div>
       <div class="scroll-pane">    
          <?php include 'load_comments.php';?>  
        </div>

        <div class="commentupdate"  id='commentbox<?php echo $_row["id"];?>'>

           <div class="stcommenttext" >
                <form method="post" action="">
                     <textarea name="comment" class="comment" maxlength="200"  id="ctextarea<?php echo $_row["id"];?>"></textarea>
                     <br />
                     <input type="submit"  value=" Comment "  id="<?php echo $_row["id"];?>" class="comment_button button"/>
                 </form>
            </div>
        </div>
  </div>

$_row['id']是一个 sql 获取数组

加载评论.php

<?php

   $commentquery = mysql_query("SELECT * FROM comments WHERE msgid=".$_row['id']."");
   $count=mysql_num_rows($commentquery);
      if($count!==0)
      {
         while($commrow = mysql_fetch_array($commentquery))
       {
      ?>
         <div class="stcommentbody" id="stcommentbody<?php echo $commrow['msgid']; ?>">
             <div class="stcommentimg">
             <?php  $userdata = mysql_query('SELECT firstname,lastname,Photo FROM users WHERE id="'.$commrow["uic"].'"' );  
                    $userrow = mysql_fetch_array($userdata); ?>
                    <img src="profile/<?php echo $userrow['Photo']; ?>" class='small_face' alt='<?php echo $userrow['firstname']; echo'&nbsp;';echo $userrow['lastname']; ?>'/>
             </div> 
          <div class="stcommenttext">
        <?php if($commrow['uic']==$uid) { ?>
            <a class="stcommentdelete" href="#" id='<?php echo $commrow['msgid']; ?>' title='Delete Comment'></a>
         <?php } ?>
        <b><a href="profile.php?id=<?php echo $commrow['uic']; ?>"><?php echo $userrow['firstname']; echo'&nbsp;';echo $userrow['lastname']; ?></a></b> <?php echo $commrow['comment'];?>
         <div class="stcommenttime"></div> 
      </div>
   </div>
  <?PHP
    }
  }else{
     echo 'Be the first to comment';
   }
?>

现在,在单击提交按钮时,将调用以下 ajax,

<script>

$(document).ready(function()  {

//Commment Submit

    $('.comment_button').on("click",function(){         

        var ID = $(this).attr("id");

        var comment= $("#ctextarea"+ID).val();
        var dataString = 'comment='+ comment + '&msgid=' + ID;

        if($.trim(comment).length===0)
        {
            alert("Please Enter Comment Text");
        }
        else
        {
            $.ajax({
                type: "POST",
                url: "comment_ajax.php",
                data: dataString,
                cache: false,
                success: function(html){
                  $(".scroll-pane").load("load_comments.php");
                }
            });
        }
        return false;
    });

      // commentopen
    $('.commentopen').on("click",function()
    {
        var ID = $(this).attr("id");
        $("#commentbox"+ID).slideToggle('fast');
        return false;

    });
    $('.close').on("click",function()
    {
        var ID = $(this).attr("id");
        $("#commentbox"+ID).hide('fast');
        return false;

    });
  });  

</script>

ajax 运行良好,评论被插入,但新评论没有被加载,而是错误:

 Notice: Undefined variable: _row in C:\wamp\www\fresh\final\load_comments.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0000  147616  {main}( )   ..\load_comments.php:0

( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\fresh\final\load_comments.php on line 5
Call Stack
#   Time    Memory  Function    Location
1   0.0000  147616  {main}( )   ..\load_comments.php:0
2   0.0030  154344  mysql_num_rows ( )  ..\load_comments.php:5

( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\fresh\final\load_comments.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0000  147616  {main}( )   ..\load_comments.php:0
2   0.0040  154472  mysql_fetch_array ( )   ..\load_commen

请帮我更正代码如果不可能,请指导我一个更好的

提前致谢 !

4

2 回答 2

0

Load_comments.php取决于$_row['id'],当您加载Load_comments.php文件时,您会将其脱离上下文,因此您可能会变$_row['id']

你可能想做这样的事情:

if (!empty($_row['id']))
   $msg_id = $_row['id'];
else if (!empty($_POST['msgid]))
   $msg_id = (int)$_POST['msgid];
$commentquery = mysql_query("SELECT * FROM comments WHERE msgid=". $msg_id ."");
于 2013-11-07T18:20:12.847 回答
0

在你的Load_comments.php而不是

$_row['id']

$_POST['msgid']

所以这条线看起来像这样:

$commentquery = mysql_query("SELECT * FROM comments WHERE msgid=".$_POST['msgid']."");

但请注意,这容易受到SQL 注入的影响。为了避免这种情况,您需要像这样转义所有输入变量:

mysql_real_escape_string($_POST['msgid'])

但由于mysql不推荐使用扩展,最好使用PDOmysqli

于 2013-11-07T18:20:43.040 回答