我的PHP消息系统向所有朋友发消息?

时间:2012-12-22 09:02:02

标签: php html mysql pdo

我正在开发一个PHP消息传递系统,但是我遇到了一个问题,无论如何我似乎无法解决这个问题,并且我一直试图修复它几个小时。

继承我的代码:

function show_friends($db){
    $friends = Array();
    $email = $_SESSION['ml'];
    $check = $db->prepare("SELECT ID FROM users WHERE Email = ?");
    $check->execute(array($email));
    $line = $check->fetch(PDO::FETCH_ASSOC);
    $ID = $line['ID'];
    $query = $db->prepare("SELECT * FROM friends WHERE UserID = ?");
    $query->execute(array($ID));
    echo "<b>Friends:</b> <br/>";
    foreach($query as $row){
        $FriendID = $row['FriendID'];
        $sentfrom = $row['SentFrom'];
        if($row['Status'] === "Friends"){
            $friend = $row['FriendName'];
            $html = '
                <html>
                    <form action method = "post" >
                        <input type = "submit" value = '.$friend.' name = '.$FriendID.' />
                        <br/>
                    </form>
                </html>
            ';
            echo $html;
            $friends[] = $FriendID;
        }
    }
    $counter = 0;
    $iterations = count($friends);
    while($counter <= $iterations){
        $person = $friends[$counter];
        if(isset($_POST[$person])){
            return $person;
        }
        $counter += 1;
    }
}

function message($message, $to, $from, $db){
    $query2 = $db->prepare("INSERT INTO messages (Message, ToID, FromID, Time) VALUES(?, ?, ?, ?)");
    $query2->execute(array($message, $to, $from, date("Y-m-d h:i:s")));
}

然后使用它的页面是:

$person = show_friends($db);
if(isset($person)){
    $html = '
        <html>
            <form action method = "post" >
                Message '.$person.' <input type = "text" name = "message" />
                <input type = "submit" value = "Message!" name = "sendto'.$person.'" />
            </form>
        </html>
    ';
    echo $html;
}
if(isset($_POST['sendto'.$person])){
    $message = $_POST['message'];
    if($message != "" or $message != null){
        message($message, $person, $ID, $db);
    }
}

每当我向朋友发送消息时,消息都会发送给我的所有朋友,如果我尝试返回某些内容,它只会将其发送给朋友阵列旁边的第一位朋友,任何想法?在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

使用WHERE语句中的INSERT INTO子句和朋友的姓名

Eg: WHERE `name`='friendname'

使用

   INSERT INTO messages (Message, ToID, FromID, Time) VALUES(?, ?, ?, ?) WHERE `to`='$to'