0

我正在开发一个 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);
    }
}

每次我向朋友发送消息时,消息都会发送给我所有的朋友,如果我尝试返回某些内容,它只会将其发送给朋友数组中的第一个朋友,知道吗?在此先感谢您的帮助。

4

1 回答 1

0

在语句中使用您朋友的名字的WHERE子句INSERT INTO

Eg: WHERE `name`='friendname'

利用

   INSERT INTO messages (Message, ToID, FromID, Time) VALUES(?, ?, ?, ?) WHERE `to`='$to'
于 2012-12-22T09:08:06.313 回答