سؤال

I am trying to set up a feature very similar to a forum, where users can write chapters or stories (long sections of text). This text is then to be stored on mysql so that it can be recalled an read and commented on etc. This is all set up and works fine, however when a new line is typed by the user, it is not recognized. So the user may type -

This is para 1

This is para 2

But all that is displayed is -

This is para1This is para 2

I know that there are certain character is html and php such as

or \n, but the people using the forum will not be savvy enough to do this. Is there a way that I can get this working automatically?

Below is the code for my add_topic.php

<?php
session_start();
$uname = $_SESSION['uname'];



$host="mysql.******************.co.uk"; // Host name 
$username="**********"; // Mysql username 
$password="************"; // Mysql password 
$db_name="************_members"; // Database name 
$tbl_name="forum_question"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get data that sent from form 
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['username'];
$datetime=date("d/m/y h:i:s"); //create date time

$sql="INSERT INTO $tbl_name(topic, detail, name, datetime)VALUES('$topic', '$detail', '$name', '$datetime')";
$result=mysql_query($sql);

if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysql_close();
?>

Currently the 'details' field on the database is set to MidText. I have also tried MidBlob but there is no change in the result.

The following is my create_topic.php code -

<?php
  session_start();
$uname = $_SESSION['uname'];
  ?>

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="add_topic.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3" bgcolor="#E6E6E6"><strong>Create New Story</strong> </td>
</tr>
<tr>
<td width="14%"><strong>Title</strong></td>
<td width="2%">:</td>
<td width="84%"><input name="topic" type="text" id="topic" size="50" /></td>
</tr>
<tr>
<td valign="top"><strong>Story</strong></td>
<td valign="top">:</td>
<td><textarea maxlength="2000000" name="detail" cols="50" rows="30" id="detail"></textarea></td>
</tr>
<tr>
<td><strong>Username</strong></td>
<td>:</td>
<td><input name="username" value="<?php echo $uname; ?>" type="text" id="username" size="50" readonly></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

I am an absolute beginner but I will try my best to make heads or tales of any advice that can be given. If you need anymore info please let me know.

Many thanks in advance.

هل كانت مفيدة؟

المحلول

Have you tried using nl2br before display? It will transform all the \n character to html <br/>.

See also the docs: http://php.net/manual/en/function.nl2br.php

نصائح أخرى

Please try

$detail=htmlentities($_POST['detail']);

to get special HTML characters transformed in your database. Hope that helps.

For further info go to http://php.net/manual/en/function.htmlentities.php

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top