传递消息时出错我自己的PHP聊天

时间:2013-11-17 03:32:04

标签: php mysql

请帮助我,我正在聊天,但只有发送邮件的用户才能看到它。另一个不能,例如:

以David身份登录:
(不能上传照片)
-ME:你好 -ME:Lalalala

登录为Shinny:
-ME:Hihihi

我认为应该在我的聊天中显示什么shinny writed,以及我的写作应该在shinny的聊天中显示,但是传入的消息没有显示
怎么样呢 -ME:你好 -Shinny:Hihihi
-ME:Lalalala

以下是代码:我该怎么做才能解决这个问题?

 <?php 

    $chaquery = "SELECT u.*,c.* FROM chats c 
     INNER JOIN users u WHERE u.user_id = c.chat_from 
     AND chat_from = '".$session['user_id']."' 
     AND chat_to = '".$_GET['id']."' 

(命运用户的id)

     OR u.user_id = c.chat_to AND u.user_id = c.chat_from
     AND chat_to = '".$session['user_id']."'
     AND chat_from = '".$_GET['id']."' 

(命运用户的ID为AGAIN)

ORDER BY chat_id ";
          $chares = mysql_query($chaquery);

            if($chares)
            {
                while($chafilas = mysql_fetch_assoc($chares))
                {
                    $me = $chafilas["chat_remit"];
                    $message = $chafilas["chat_cont"];
                    $hour = $chafilas["chat_hora"];
                    $date = $chafilas["chat_fecha"];
                    $day = $chafilas["chat_dia"];
                    $fullname = $chafilas["usuario_nombre"];
                    $nick = $chafilas["usuario_nick"];
                    $userphoto = $chafilas["usuario_foto"];

                    if($me){?><div class="panel-body">
                     <img src="avatar/<?php echo $tufoto;?>" style="float:left; margin-right:10px;" width="55px" class="ui"/>
                     <a href="<?php echo $xnick;?>"><b style="font-size:16px;"><?php echo $xnombre;?></b></a><br><?php echo $xmensaje;?>
                    </div><?php }}}?>

这里的表

  `chat_id` int(11) NOT NULL AUTO_INCREMENT,
  `chat_from` varchar(45) COLLATE utf8_bin NOT NULL,
  `chat_to` varchar(45) COLLATE utf8_bin NOT NULL,
  `chat_message` longtext COLLATE utf8_bin NOT NULL,
  `chat_hour` varchar(10) COLLATE utf8_bin NOT NULL,
  `chat_day` varchar(2) COLLATE utf8_bin NOT NULL,
  `chat_date` varchar(7) COLLATE utf8_bin NOT NULL,
  `chat_seen` varchar(2) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`chat_id`)

2 个答案:

答案 0 :(得分:0)

您的代码没有任何问题......您是否将这些聊天内容保存在某处?如果是的话,你必须刷新包含聊天消息的特定div,使用ajax调用这样的东西......

<script type="text/javascript">
$(function() {
//populating chat the first time
refresh_chat();
// recurring refresh every 1 seconds
setInterval("refresh_chat()", 1000);

    $("#send").click(function() {
        // getting the values that user typed
        var send    = $("#send").val();
        var chat    = $("#chat").val();
        var data   = 'add='+ add + '&comp='+ comp;

      $.ajax({
            type: "POST",
            url: "chat.php",
            data: data,
            success: function(html){ 
            // this happen after we get result
                $("#scroll").fadeOut(500, function(){
                    $(this).html(html).fadeIn(500);
                    $("#scroll").val("");
                    //alert('hello');

                        });


            });   
             return false;
            }
function refresh_shoutbox() {
    var data = 'refresh=1';
$.ajax({
        type: "POST",
        url: "chat.php",
        data: data,
        success: function(html){ // this happen after we get result
            $("#chat").html(html);
        }
    });

    });


});
</script> 

希望这有助于你:)

答案 1 :(得分:0)

使用UNION,您可以执行2次查询以获得所需的结果

SELECT * FROM (
  // Get messages to
  SELECT u1.* ,c1.* FROM chats c1
  INNER JOIN users u1
  ON u1.user_id = '".$session['user_id']."' 
  AND c1.chat_to = '".$session['user_id']."'
  AND c1.chat_from = '".$_GET['id']."' 

  UNION

  // Get messages from user  
  SELECT u2.* ,c2.* FROM chats c2
  INNER JOIN users u2
  ON u2.user_id = '".$_GET['id']."'
  AND c2.chat_to = '".$_GET['id']."'
  AND c2.chat_from = '".$session['user_id']."' 

) result
ORDER BY chat_id

SQLFiddle示例 - http://sqlfiddle.com/#!2/3d2af7/29


请注意,
(1)您可以直接在查询中使用$_GET值进行sql注入,并且 (2)来自mysql_ doc - 从PHP 5.5.0开始不推荐使用此扩展,并且将来会被删除。相反,应该使用MySQLi或PDO_MySQL扩展。有关详细信息,另请参阅MySQL: choosing an API指南和related FAQ