【问题标题】:two users conversation URL in php mysqliphp mysqli 中的两个用户对话 URL
【发布时间】:2020-04-01 09:42:14
【问题描述】:

我正在 PHP mysqli 中创建两个用户对话视图脚本。我的脚本如果其他用户的最后一条消息它的 URL 打开,但是当我作为最后一条消息发送时,它没有打开,因为它显示了我的 id,因此 我想在没有我的 id 的情况下获取另一个用户 id 到 URL,

对不起,我的英语不好。

我的数据库下午表

id  from_id    to_id     msg               sent_date
1   2          3         hi how are you?   2019-12-05 04:14:20
2   3          2         fine              2019-12-05 05:15:58
3   2          3         hi                2019-12-05 03:20:34
4   5          2         hi                2019-12-05 08:30:40

网址

<a href="cons.php?to_id=<?php echo $row['from_id'];?">Replay</a>

这是我的源代码

  <?php
    require_once"config.php";
    if (isset($_SESSION['userid'])) {
    $to_id = $_SESSION['userid'];  
    }

    if ($stmt = $con->prepare("SELECT * FROM pm WHERE from_id = ? OR to_id = ?  ORDER BY sent_time DESC")) {
        $stmt->bind_param('ii', $to_id, $to_id);
        $stmt->execute();
    }

    $tempArray = array();

    $result = $stmt->get_result();
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {

          if (!in_array($row['to_id'].$row['from_id'], $tempArray)) {
            echo "<br>";
            echo $row['from_id']." - " . $row['to_id']." ". $row['msg']. " - " .$row['sent_time'];


    ?>

 <a href="cons.php?to_id=<?php echo $row['from_id'];?">Replay</a>

    <?php }?>
    <?php


          array_push($tempArray, $row['from_id'].$row['to_id']);
          array_push($tempArray, $row['to_id'].$row['from_id']);

        }
    } else {
        echo "NO MESSAGES";
    }

    ?>

【问题讨论】:

  • 无法理解 ;)。你想在这里说什么
  • 你可以用印地语或英式英语告诉我你的观点。
  • 你想在href中获取谁的ID?
  • 当我讨论另一个用户的最后一条消息时,如果已发送,我无法打开覆盖,因为它显示了我的 ID,因此我想让另一个用户将其发送到 url
  • 当我发送最后一条消息时我的问题没有打开,但是如果发送给我的最后一条消息它的 url 打开另一个用户。

标签: php session mysqli


【解决方案1】:

同时更改您的网址。

<a href="cons.php?to_id=<?php if(isset($_GET['to_id'])) {$to_id=$_GET['to_id']; echo $to_id;}?>">Replay</a>

使用此代码。

<?php
    require_once"config.php";
    if (isset($_SESSION['userid'])) {
    $from_id = $_SESSION['userid'];  
    }
    if (isset($_GET['to_id'])) {
    $to_id= $_GET['to_id'];  
    }

    if ($stmt = $con->prepare("SELECT * FROM pm WHERE from_id = ? OR to_id = ?  ORDER BY sent_time DESC")) {
        $stmt->bind_param('ii', $from_id, $to_id);
        $stmt->execute();
    }

    $tempArray = array();

    $result = $stmt->get_result();
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {

          if (!in_array($row['to_id'].$row['from_id'], $tempArray)) {
            echo "<br>";
            echo $row['from_id']." - " . $row['to_id']." ". $row['msg']. " - " .$row['sent_time'];


    ?>

 <a href="cons.php?to_id=<?php echo $row['from_id'];?">Replay</a>

    <?php }?>
    <?php


          array_push($tempArray, $row['from_id'].$row['to_id']);
          array_push($tempArray, $row['to_id'].$row['from_id']);

        }
    } else {
        echo "NO MESSAGES";
    }

    ?>

【讨论】:

  • 获得相同的结果
  • 您可以回复自己的消息,但不能回复对方的消息。是你的问题吗?
  • 我太糊涂了,你能把你的代码发到 info@socialcodia.com 给我们
  • 我的代码问题,当我与其他用户讨论时,如果我已将消息作为最后一条消息发送,则我的用户 id 会显示在 url 上,但我想讨论其他用户 id 以获取 url 我的 id 1你可以检查输出图像
  • 我检查了兄弟,但结果相同
【解决方案2】:

希望我这次明白了

这是我的代码:

  if (isset($_SESSION['userid'])) {
    $session_id = $_SESSION['userid'];
  }

  if ($stmt = $con->prepare("SELECT * FROM pm WHERE from_id = ? OR to_id = ? ORDER BY sent_date DESC")) {
    $stmt->bind_param('ii', $session_id, $session_id);
    $stmt->execute();
  }

  $tempArray = array();

  $result = $stmt->get_result();

  if ($result->num_rows > 0) {
      while ($row = $result->fetch_assoc()) {

        if (!in_array($row['to_id'].$row['from_id'], $tempArray)) {

          echo "<br>";
          echo $row['from_id']." - " . $row['to_id']." ". $row['msg']. " - " .$row['sent_time'];


        $guaranteed_from_id = str_replace($session_id, null, $row['to_id'].$row['from_id']);

        ?>

        <a href="cons.php?to_id=<?php echo $guaranteed_from_id ?>">Replay</a>

        <?php

 }


        array_push($tempArray, $row['from_id'].$row['to_id']);
        array_push($tempArray, $row['to_id'].$row['from_id']);

      }
  } else {
      echo "NO MESSAGES";
  }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-03-13
  • 2016-10-08
  • 2015-03-16
  • 2018-05-03
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 2014-12-28
相关资源
最近更新 更多