允许在 php 聊天中换行 - MySQL PHP

发布于 2024-12-15 01:40:58 字数 1846 浏览 3 评论 0原文

我构建了一个非常简单的聊天系统...它工作得很好,当用户输入消息时它会正确显示,唯一的问题是换行符不会显示...在将信息发送到的过程中的某个地方数据库并检索它,我丢失了换行符...

我想我需要将 \n 转换为
一路上的某个地方,但我真的不这样做对这种类型的编码了解很多...我主要做游戏之类的...没有网站或直到今天(我的第一天)数据库编码

这是我在 chat.php 上找到的代码

<html><head></head><body>
<form action="chat.php" method="post">
Message: <br><textarea type="text" name="message" style="width:80%; height:300px;"></textarea><br>
<input type="submit" />
</form>
<div style="width:100%;">

<?php

$host="***";
$user="***";
$password="***";

$cxn = mysql_pconnect ($host, $user, $password);

mysql_select_db("defaultdb", $cxn);

if (getenv(HTTP_X_FORWARDED_FOR)) {
    $ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
    $ipaddress = getenv(REMOTE_ADDR);
}

$message = strip_tags($_POST["message"]);

mysql_query("INSERT INTO ChatTest (ID, TimeStamp, Message) VALUES ('$ipaddress', NOW(), '$message')");

$data = mysql_query("SELECT * FROM ChatTest ORDER BY TimeStamp DESC") or die(mysql_error()); 
 Print "<table border cellpadding=3 width='100%' style='table-layout:fixed'>
        "; 
 Print "<tr>"; 
 Print "<th style='width:10%;'>ID:</th><th style='width:10%;'>TimeStamp:</th><th style='width:70%;'>Message:</th>";
 while($info = mysql_fetch_array( $data )) { 
 Print "
        <tr>"; 
    Print " <td>".$info['ID'] . "</td> "; 
    Print " <td>".$info['TimeStamp'] . " </td>";
    Print " <td style='white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word'>".$info['Message'] . "</td></tr>
            "; 
 } 
 Print "</table>"; 

mysql_close($cxn);


?>

</div></body></html>

I built a very simple chat system... it works great and when the user enters a message it displays properly, the only problem is that line breaks don't show up.... somewhere along the line of sending the info to the database and retrieving it I lose the line breaks...

I am thinking I need to convert \n to <br> somewhere along the way but I really don't know much about this type of coding... I've mostly done games and stuff... No websites or database coding until today (my first day)

Here is my code found on chat.php

<html><head></head><body>
<form action="chat.php" method="post">
Message: <br><textarea type="text" name="message" style="width:80%; height:300px;"></textarea><br>
<input type="submit" />
</form>
<div style="width:100%;">

<?php

$host="***";
$user="***";
$password="***";

$cxn = mysql_pconnect ($host, $user, $password);

mysql_select_db("defaultdb", $cxn);

if (getenv(HTTP_X_FORWARDED_FOR)) {
    $ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
    $ipaddress = getenv(REMOTE_ADDR);
}

$message = strip_tags($_POST["message"]);

mysql_query("INSERT INTO ChatTest (ID, TimeStamp, Message) VALUES ('$ipaddress', NOW(), '$message')");

$data = mysql_query("SELECT * FROM ChatTest ORDER BY TimeStamp DESC") or die(mysql_error()); 
 Print "<table border cellpadding=3 width='100%' style='table-layout:fixed'>
        "; 
 Print "<tr>"; 
 Print "<th style='width:10%;'>ID:</th><th style='width:10%;'>TimeStamp:</th><th style='width:70%;'>Message:</th>";
 while($info = mysql_fetch_array( $data )) { 
 Print "
        <tr>"; 
    Print " <td>".$info['ID'] . "</td> "; 
    Print " <td>".$info['TimeStamp'] . " </td>";
    Print " <td style='white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word'>".$info['Message'] . "</td></tr>
            "; 
 } 
 Print "</table>"; 

mysql_close($cxn);


?>

</div></body></html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红尘作伴 2024-12-22 01:40:58

使用 nl2br() 将普通换行符转换为 html 换行符。

Use nl2br() to convert plain linebreaks to html linebreaks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文