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

时间:2022-12-12 07:15:40

Please help me, i'm working in a chat, but only the user who sent the message, can see it. the other one cant, example:

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

Logged in as David:
(cant upload photos)
-ME: Hello
-ME: Lalalala

登录为David :(无法上传照片)-ME:Hello -ME:Lalalala

Logged in as Shinny:
-ME: Hihihi

登录为Shinny:-ME:Hihihi

it is supposed that what shinny writed should be displaying in my chat, and what im writing should display on shinny's chat, but the incoming message is not showing
How it should be
-ME: Hello
-Shinny: Hihihi
-ME: Lalalala

假设shinny writed应该在我的聊天中显示,而且我的写作应该在shinny的聊天中显示,但是传入的消息没有显示它应该是-ME:你好 - 谢谢:Hihihi -ME:Lalalala

Here is the code: What can i do to solve this thing?

这是代码:我能做些什么来解决这个问题?

 <?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 of the destiny user)

(命运用户的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 of the destiny user AGAIN)

(命运用户的ID)

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 }}}?>

Here THE TABLE

这里的表

  `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 个解决方案

#1


0  

there is nothing wrong with your code..are you saving these chat content somewhere?? if yes you have to refresh that particular div which contain your chat msgs using ajax call something like this....

您的代码没有任何问题......您是否在某处保存这些聊天内容?如果是的话,你必须刷新包含你的聊天消息的特定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> 

hope this helps u :)

希望这有助于你:)

#2


0  

Using UNION, you can do 2 queries to get the results you want

使用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 Example - http://sqlfiddle.com/#!2/3d2af7/29

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


Note,
(1) you are open to sql injection by using $_GET values directly in your query, and
(2) from the mysql_ doc - This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

注意,(1)您可以直接在查询中使用$ _GET值来打开sql注入,并且(2)从mysql_ doc开始 - 从PHP 5.5.0开始不推荐使用此扩展,并且将来会被删除。相反,应该使用MySQLi或PDO_MySQL扩展。另请参阅MySQL:选择API指南和相关的常见问题解答以获取更多信息。

#1


0  

there is nothing wrong with your code..are you saving these chat content somewhere?? if yes you have to refresh that particular div which contain your chat msgs using ajax call something like this....

您的代码没有任何问题......您是否在某处保存这些聊天内容?如果是的话,你必须刷新包含你的聊天消息的特定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> 

hope this helps u :)

希望这有助于你:)

#2


0  

Using UNION, you can do 2 queries to get the results you want

使用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 Example - http://sqlfiddle.com/#!2/3d2af7/29

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


Note,
(1) you are open to sql injection by using $_GET values directly in your query, and
(2) from the mysql_ doc - This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

注意,(1)您可以直接在查询中使用$ _GET值来打开sql注入,并且(2)从mysql_ doc开始 - 从PHP 5.5.0开始不推荐使用此扩展,并且将来会被删除。相反,应该使用MySQLi或PDO_MySQL扩展。另请参阅MySQL:选择API指南和相关的常见问题解答以获取更多信息。