Mysql公共用户聊天给朋友聊天

时间:2015-02-11 10:35:08

标签: php html mysql chat

我目前按照公共用户聊天的教程使用Msg_ID,Sender,Message。发件人是这个名字。我目前有一个朋友表,其中: ID,my_id,friend_id。 但我不确定如何改变聊天,所以它只发送给收件人,反之亦然。我已将聊天表更改为以下内容:Msg_id,Author_id,Recipient_id,Message。 聊天的当前查询是:

<?php

    function get_msg() {

        $query = "SELECT `Author_ID`, `Message` FROM `pdo_ret`.`chat` ORDER BY `Msg_ID` ASC";

        $run = mysql_query($query);

        $messages = array();

        while($message = mysql_fetch_assoc($run)) {
            $messages[] = array('Author_ID'=>$message['Author_ID'],
                                'message'=>$message['Message']);
        }

        return $messages;

    }

    function send_msg($Author_ID, $message) {

        if(!empty($Author_ID) && !empty($message)) {

            $Author_ID  = mysql_real_escape_string($Author_ID);
            $message    = mysql_real_escape_string($message);

            $query = "INSERT INTO `pdo_ret`.`chat` (Msg_ID, Author_ID, Message) VALUES (null, '{$Author_ID}', '$message')";

            if($run = mysql_query($query)) {
                return true;
            } else {
                return false;
            }

        } else {
            return false;
        }
    }

?>

聊天表格是:

                        <form action="#" method="post" onsubmit="return InsertDefaultValues()"  id="form_input">
                            <input hidden type="text" name="Author_ID" id="Author_ID"/>
                            <input hidden type="text" name="friend_id" id="friend_id"/>
                            <input type="text" id="message" cols="5" rows="4"></textarea>
                        <input type="submit" name="send"  onsubmit="return InsertDefaultValues()"  id="send" value="Send Message"/>
                        </form>

以下是我尝试用来定义登录用户的会话脚本。

&#13;
&#13;
<?php
session_start();
if(!isset($_SESSION["user"]) or !is_array($_SESSION["user"]) or empty($_SESSION["user"]));
?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

应对查询进行以下更改:

仅检索您的消息:

$query = "SELECT `Author_ID`, `Message` FROM `pdo_ret`.`chat` WHERE Recipient_id = '$Recipient_id' ORDER BY `Msg_ID` ASC";

指定配方:

$query = "INSERT INTO `pdo_ret`.`chat` (Msg_ID, Author_ID, Recipient_id, Message) VALUES (null, '{$Author_ID}', '$Recipient_id', '$message')";