2016-05-04 3 views
0

Ich versuche, einen Chat-Raum mit PHP zu machen (es funktioniert BTW), aber nur die Nachrichten werden angezeigt, nicht ihre Benutzernamen. Ich habe die Datenbanken für sie erstellt, Benutzername und msg. Ich weiß nicht, warum ihre Benutzernamen nichtPHP ChatRoom. Benutzername wird nicht angezeigt, nur Nachrichten

<? 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>

Anzeigen sind. Zögern Sie nicht, auf die Fehler hinzuweisen.

Antwort

2

ersetzen

insert.php?uname 

mit

insert.php?uname= 

Aktualisiert Code:

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

Sie sind nicht uname

+0

Guter Fang Alexis Einfügen, +1 – Pupil