PHP 聊天室。不显示用户名,仅显示消息

标签 php html mysql ajax apache

我正在尝试使用 PHP 创建一个聊天室(顺便说一句,它正在工作),但只显示消息,而不显示他们的用户名。我已经为他们创建了数据库、用户名和消息。我不知道为什么他们的用户名没有显示

<? php

//$uname = $_REQUEST['uname'];
//$msg = $_REQUEST['msg'];

$uname = (isset($_REQUEST['uname']) ? $_REQUEST['uname'] : '');
$msg = (isset($_REQUEST['msg']) ? $_REQUEST['msg'] : null);





$con = mysql_connect('localhost', 'root', '');
mysql_select_db('chatbox', $con);

mysql_query("INSERT INTO logs (`username` , `msg`) VALUES ('$uname', '$msg')");

$result1 = mysql_query("SELECT * FROM logs ORDER by id DESC");

while ($extract = mysql_fetch_array($result1)) {
  echo "<span class = 'uname'>".$extract['username'].
  "</span>: <span class = 'msg'> ".$extract['msg'].
  "</span><br/>";
}

?>
<?php $con=mysql_connect('localhost',
'root',
'');
 mysql_select_db('chatbox',
$con);
 $result1=mysql_query("SELECT * FROM logs ORDER by id DESC");
 while($extract=mysql_fetch_array($result1)) {
  echo"<span class = 'uname'>" . $extract['username'] ."</span>: <span class = 'msg'> " . $extract['msg'] ."</span><br/>";
}
?>
<?php ?>

<html>

<head>
  <script src="http://code.jquery.com/jquery-1.9.0.js"></script>
  <style>
    * {
      margin: 0;
    }
    body {
      padding-right: 250px;
      padding-left: 250px;
      margin: 0;
      background-color: darkkhaki;
    }
    textarea {
      resize: none;
      width: 100%;
      height: 300px;
      background-color: #efefef;
    }
    a {
      background-color: cadetblue;
      padding: 15px 25px 15px 25px;
      text-decoration: none;
      color: white;
    }
    a:hover {
      background-color: chartreuse;
    }
  </style>
</head>

<body>
  <form name="form1">
    Enter your Username
    <input type="text" name="uname">
    <br/>Enter your Message
    <br/>
    <textarea name="msg"></textarea>
    <br/>
    <br/>
    <a href="#" onclick="">Send</a>
    <br/>
    <br/>
    <div id="chatlogs">
      Loading Chat...
    </div>
  </form>

  <script>
    function submitChat() {
      if (form1.uname.value == '' || form1.msg.value == '') {
        alert('Please Input Username and Message');
        return;
      }

      var uname = form1.uname.value;
      var msg = form1.msg.value;
      var xmlhttp = new XMLHttpRequest();

      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;
        }
      }

      xmlhttp.open('GET', 'insert.php?uname' + uname + '&msg=' + msg, true);
      xmlhttp.send();

    }
  </script>
</body>

</html>

。错误之处欢迎指出。

最佳答案

替换

insert.php?uname

insert.php?uname=

更新的代码:

xmlhttp.open('GET', 'insert.php?uname=' + uname + '&msg=' + msg, true);

您没有插入 uname

关于PHP 聊天室。不显示用户名,仅显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37027425/

相关文章:

php - 无法将数据库值 "1876-01-01 00:00:00-00:00:00"转换为 Doctrine 类型日期时间

php - Laravel 5 内置用户身份验证中的重定向

php - JavaScript Date 对象是否将输入的日期字符串识别为 UTC 时间或本地时间?

php - 以 root 身份运行 php 脚本

html - 如何降低单元格的高度?

javascript - 如何根据下拉选择填充文本值类型的输入 - Angular 5

html - 纯css3视差和移动

mysql - 集合映射的 where 子句

c# - Microsoft.SqlServer.Replication.ComErrorException;无法将属性更改为 "Any CPU"

mysql - sum() 列用于相同 id 的多个查询并显示到连接表