php mysqli中两个用户之间的消息对话

时间:2019-11-29 17:26:40

标签: php mysqli

我正在创建简单的私人消息对话脚本。我的脚本显示了传入用户的对话。但是我发送给另一个用户的消息没有显示。 任何人都可以帮助我,我已经尝试了很多。

我的数据库

id  from_id  from_name  to_id   to_name  msg
1   2        master     3       john     hi how are you?
2   3        john       2       master   fine
3   2        master     3       john     hi

这是我的代码对话

<?php
if (isset($_GET['to_id'])) {
$from_id = $_GET['to_id'];  
}

if (isset($_GET['to_name'])) {
$from_name = $_GET['to_name'];  
}

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

require_once"config.php";
if ($stmt = $con->prepare("SELECT * from inbox where from_id=? and from_name=? and to_id=? ")){
$stmt->bind_param('sss', $from_id,$from_name,$to_id);
$stmt->execute();
}
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>

<div class="msg">
<?php echo $row['from_name'];?>
<?php echo $row['msg'];?>
<?php 
}
}
?>
</div>

output image here

1 个答案:

答案 0 :(得分:0)

检查此代码

<?php
if (isset($_GET['to_id'])) {
$from_id1 = $_GET['to_id'];  
}

if (isset($_GET['to_name'])) {
$from_name1 = $_GET['to_name'];  
}

if (isset($_SESSION['userid'])) {
$to_id1 = $_SESSION['userid'];  
}
require_once"config.php";
if ($stmt = $con->prepare("SELECT * from inbox where (from_id='2' and to_id='3' and from_name ='jhon') || (from_id='3' and to_id='2')")){
$stmt->bind_param('sss', $from_id,$from_name,$to_id);
$stmt->execute();
}
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>

<div class="msg">
<?php echo $row['from_name'];?>
<?php echo $row['msg'];?>
<?php 
}
}
?>
</div>