【发布时间】:2017-08-21 12:38:16
【问题描述】:
我在 PHP 中做了一个私人消息系统。 它有点工作,写、发送、读和回复工作。
但是有一个问题。如果我按下我想在收件箱或发件箱中阅读的特定消息(无论哪个),我的页面应该只显示我按下的特定消息,显示该收件箱/发件箱中的所有消息。在我的页面上看起来像这样 ->
From:testing@l.seSubject:hello Message:Testing this.. From:testing@l.se 主题:你好 消息:测试 发件人:testing@l.se 主题:hej 消息:skicka :回复
你们都可以看到,它的所有消息都是连续的。编辑:它消息属于正确的用户。所以它不是属于不同用户的消息。
我的消息的 sql 是
id int(11) AI PK
from_user varchar(45)
to_user varchar(45)
subject varchar(400)
message text
date date
read tinyint(4)
我很确定我的错误应该在此处的某个地方
读取.inc.php
<?php
$user = $_SESSION['username'];
$sql = "SELECT * FROM private_messages WHERE to_user = '$user'";
$stmt = $dbh->prepare($sql);
$stmt->execute();
?>
<?php
if ($stmt->rowCount() > 0){
echo "<table";
echo "<tr>";
while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)){
$id = $rows['id'];
$to_user = $rows['to_user'];
echo "<td>";
?>
<?php
echo "<td>From:";
echo "</td>";
echo "<td>";
echo "".$from = $rows['from_user']."";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "Subject:";
echo "<td>";
echo "<td>";
echo "".$subject = $rows['subject']."";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "Message:";
echo "<td>";
echo "".$message = $rows['message']."";
echo "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan='2'><a href='messages.php?
id=compose&mid=$id&subject=RE:$subject&to=$from'>Reply Message</a>
</td>";
echo "</tr>";
echo "</table>";
if ($to_user==$user) {
$stmt = $dbh->prepare("UPDATE `private_messages` SET `read`=1 WHERE
`id`=id");
$a = 1;
$stmt->bindParam(':1',$a);
$stmt->bindParam(':id',$id);
}
} else {
echo "You cant see the conversation..";
}
?>
如果有人想看的话,我也会粘贴收件箱和发件箱。
这是 outbox.inc.php
<?php
$user = $_SESSION[ 'username' ];
$sql = "SELECT * FROM private_messages WHERE from_user = '$user'";
$stmt = $dbh->prepare( $sql );
$stmt->execute();
?>
<?php
if ( $stmt->rowCount() > 0 ) {
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
echo "<table>";
echo "<tr>";
echo "<td> ";
echo "</td>";
echo "<td>to: </td>";
echo "<td>subject: </td>";
echo "<td>Date: </td>";
echo "</tr>";
while ( $rows = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
$id = $rows[ 'id' ];
?>
<?php
echo "<tr>";
echo "<td> </td>";
echo "<td>" . $from = $rows[ 'to_user' ] . "</td>";
echo "<td><a href='messages.php?id=read&mid=$id'>" . $subject = $rows[
'subject' ] . "</a></td>";
echo "<td>" . $date = $rows[ 'date' ] . "</td>";
echo "<tr>";
}
}
else {
echo "<table> <tr align='left'> <td> </td> <td>to_user: </td><td>
Subject: </td><td>Date: </td></tr><tr><th colspan='4'> You did not send a
message </th></tr></table>";
}
echo "</table>";
?>
</body>
</html>
最后我的
收件箱.inc.php
<?php
$user = $_SESSION[ 'username' ];
$sql = "SELECT * FROM private_messages WHERE to_user = '$user'";
$stmt = $dbh->prepare( $sql );
$stmt->bindValue( ':to_user', $_SESSION[ 'username' ], PDO::PARAM_INT );
$stmt->execute();
?>
<?php
if ( $stmt->rowCount() > 0 ) {
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
echo "<table>";
echo "<tr>";
echo "<td> ";
echo "</td>";
echo "<td>from_user: </td>";
echo "<td>subject: </td>";
echo "<td>Date: </td>";
echo "</tr>";
// om stmt är större än noll då finns de poster gör då detta
// skriv ut posterna med en while loop
while ( $rows = $stmt->fetch(PDO::FETCH_ASSOC)) {
$id = $rows['id'];
echo "<tr>";
echo "<td> </td>";
echo "<td>" . $from = $rows[ 'from_user' ] . "</td>";
echo "<td><a href='messages.php?id=read&mid=$id'>" . $subject =
$rows[ 'subject'] . "</a></td>";
echo "<td>" . $date = $rows[ 'date' ] . "</td>";
echo "<tr>";
}
} else {
echo "<table> <tr align='left'> <td> </td> <td>from_user: </td>
<td> Subject: </td><td>Date: </td></tr><tr><th colspan='4'> You did not
recive a message </th></tr></table>";
}
echo "</table>";
?>
</body>
</html>
这里有很多代码,抱歉。
/最好的问候罗伯特
【问题讨论】:
-
您对所有文件使用相同的查询,即(获取给定 user_id 的所有消息)理想情况下,在查看消息时,您应该通过 $_GET 获取 message_id 确保它属于用户试图通过将其与登录用户进行比较来查看它并仅选择所需的消息。