duanan1228 2016-02-05 08:09
浏览 401
已采纳

如何在不刷新页面的情况下更新mysql数据库中的数据

I have a website and I need to put a chat box but when someone write the users has to refresh the whole page to read the text but I need to make it automatically update the data please help me.

Now this is the code:

<?
session_start();
include("includes/config.php");
if($_GET['with']){
    if($_SESSION['login']){
        if($_SESSION['login'] == $_GET['with']){
            header("Location: index.php");
        }else{
        $id = $_SESSION['login'];
        $with = intval($_GET['with']);
            if($_POST['submit']){
                $text = $_POST['text'];
                if(empty($text)){

                }else{
                    $query = mysqli_query($connect,"INSERT INTO chat(`from`,`to`,`topic`) VALUES('$id','$with','$text')");
                }
            }
        ?>
            <form method="post" action="chat.php?with=<?=$with?>">
                <textarea name="text" placeholder="Write Here..." style="text-align:right;resize:none;width:100%;height:200px;font-size:24">
                </textarea>
                <br/>
                <input type="submit" name="submit" value="Send"/>
            </form>
            <div id="chat">
        <?
        $query = mysqli_query($connect,"SELECT * FROM users WHERE id='$id'");
        $f = mysqli_fetch_array($query);
        $query = mysqli_query($connect,"SELECT * FROM users WHERE id='$with'");
        $ff = mysqli_fetch_array($query);
        $query = mysqli_query($connect,"SELECT * FROM chat order by id desc");
        while($fetch = mysqli_fetch_array($query)){
            if($fetch['from'] == $with && $fetch['to'] == $id or $fetch['from'] == $id && $fetch['to'] == $with){
                if($fetch['from'] == $f['id']){
                    echo "<div style='word-wrap: break-word;'>".$f['fname']."&nbsp;".$f['lname'].":<br/>".$fetch['topic']."</div>";
                }
                if($fetch['from'] == $ff['id']){
                    echo "<div style='max-width:200px;word-wrap: break-word;'>".$ff['fname']."&nbsp;".$ff['lname'].":<br/>".$fetch['topic']."</div>";
                }
            }
        }?>
        </div>
        <?}
    }else{
        header("Location: index.php");
    }
}else{
    header("Location: index.php");
}

?>

  • 写回答

4条回答 默认 最新

  • donglun7151 2016-02-05 08:19
    关注

    You can do it using ajax. you can check this tutorial, Chat box using ajax

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大