doupingtang9627 2017-08-21 12:38
浏览 6
已采纳

我的阅读消息页面显示多条消息而不是一条PHP

I have made a private message system in PHP. It kinda works, the write, send, read and reply works.

But there is one problem. If i press on a specific message that i want to read in my Inbox or Outbox, (dosent matter which), my page which should show just the specific message i press on, shows all messages in that inbox/outbox. It looks like this on my page ->

From:testing@l.seSubject:hello Message:Testing this.. From:testing@l.se Subject:hello Message: test From:testing@l.seSubject:hej Message: skicka :Reply

As you all can se, its all messages in a row. Edit: It messages that belongs to the right user. So its not messages that belongs to a diffrent user.

The sql for my messages are

id int(11) AI PK 
from_user varchar(45) 
to_user varchar(45) 
subject varchar(400) 
message text 
date date 
read tinyint(4)

I am fairly sure that my error should be somewhere here in

read.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..";
     }

?>

I will also paste inbox and outbox if somone feels to look there to.

This is 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>&nbsp;";
    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>&nbsp;</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>

And last my

inbox.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>&nbsp;";
        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>&nbsp; </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>

Lots of code here, sorry for that.

/best regards Robert

  • 写回答

1条回答 默认 最新

  • douzi0609 2017-08-21 12:52
    关注

    You have the following in your read.inc.php, which fetches all messages for a given user:

    $sql = "SELECT * FROM private_messages WHERE to_user = '$user'";`
    

    To only get the message that you clicked on in your inbox you should use a query like this:

    $sql = "SELECT * FROM private_messages WHERE to_user = '$user' AND id = $mid LIMIT 1";
    

    (where $mid is the variable you get from $_GET['mid'] in your query string).

    You won't need the loop then, since you only fetch one row.

    Please be aware that using variables in the query like this (especially when transmitted via query string) is VERY BAD and can/will lead to SQL injection attacks. Use bound variables (either with bindParam/bindVariable or in execute) like you did in inbox.inc.php!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?