Create a simple chat box in php so far this is what i have but it keeps showing
ID: 3911834 • Letter: C
Question
Create a simple chat box in php
so far this is what i have but it keeps showing code outside:
<html>
<head>
<title>Network Chat Room</title>
</head>
<body>
<h1>Welcome to the cave</h1>
<h1>Enter your comments below:</h2>
<form action="<? echo "$PHP_SELF"?>" method="POST">
<textarea cols=50 rows=10 name="note" wrap=virtual></textarea>
<p><input type="submit" value=" Submit "></p>
</form>
<?
$link = mysqli_connect('mysql-user','username','password', 'dbname');
$note = $_POST["note"];
if(isset($note))
{
$ts = date("F j, Y, g:i a");
$query = sprintf("insert into comments values('%s', '%s')", mysqli_real_escape_string($link, $note), $ts);
$result = mysqli_query($link, $query);
}
mysqli_close($link);
?>
<h2>Chat log:</h2>
<?
$link = mysqli_connect('mysql-user','username','password', 'dbname');
$result = mysqli_query($link, "select * from comments");
while($row = mysqli_fetch_row($result))
{
echo $row[1] ." Message: " . $row[0] . "<br> ";
}
mysqli_close($link);
?>
</body>
</html>
Explanation / Answer
<html>
<head>
<title>Network Chat Room</title>
</head>
<body>
<h1>Welcome to the cave</h1>
<h1>Enter your comments below:</h2>
<form action="<? echo "$PHP_SELF"?>" method="POST">
<textarea cols=50 rows=10 name="note" wrap=virtual></textarea>
<p><input type="submit" value=" Submit "></p>
</form>
<?
$link = mysqli_connect('localhost','username','password', 'db_name');
$note = $_POST["note"];
if(isset($note))
{
//$ts = date("F j, Y, g:i a");
$query = sprintf("insert into comments values('%s', NOW())", mysqli_real_escape_string($link, $note), $ts);
$result = mysqli_query($link, $query);
}
mysqli_close($link);
?>
<h2>Chat log:</h2>
<?
$link = mysqli_connect('localhost','username','password', 'db_name');
$result = mysqli_query($link, "select * from comments");
while($row = mysqli_fetch_row($result))
{
echo date("F j, Y, g:i a",strtotime($row[1])) ." Message: " . $row[0] . "<br> ";
}
mysqli_close($link);
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.