0

I am trying to create a pastebin type of thing. I have got my php code

<?php    
  printf("uniqid(): %s\r\n", uniqid());
?>


<!DOCTYPE html>
<meta charset="UTF-8">
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Xdev - CodeShare</title>
</head>

<form method="post" action="cslink.php">
        <textarea cols="100" rows="20" name="textbox"></textarea> 
        <br />
        <input type="submit" value="Submit now" />
</form>

and my php file to store it in my mysql database

<?php
require 'connect.php';
// Connecting to the MySQL database
$conn = mysql_connect($host, $user, $pass);
mysql_select_db($db);

$username = $_POST['textbox'];

 $sql = "INSERT INTO `text` (`id`, `text` ) VALUES (NULL, '$username' );";

$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>

and my view file (also in php)

<?php
require 'connect.php';
$sql    = 'SELECT * FROM text WHERE id = 3';
$result = mysql_query($sql, $conn);

while ($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$text = $row['text'];

echo "id: " . $id . "<br/>";
echo "text: " . $text . "<br/>";
echo $text;
}

?>

So if I type in:

hello 
world 
testing

(each on a separate line)

It will show up as this: hello world testing - all in a single line

I want to know if I can and how to output it as separate lines

Apul Gupta
  • 3,044
  • 3
  • 22
  • 30
  • 2
    P.S. ***Please*** don't actually use this code. It's very unsafe. Look into using MySQLi (or PDO) instead and also using prepared statements. – gen_Eric Oct 27 '14 at 17:36
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide.[Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Oct 27 '14 at 17:36
  • 2
    [**nl2br()**](http://php.net/manual/en/function.nl2br.php)???? – itachi Oct 27 '14 at 17:38
  • Try using PHP's `nl2br()` function. Browsers will collapse excess whitespace and newlines. – gen_Eric Oct 27 '14 at 17:38
  • i know this is unsecure but it is used for the purpose for the question. i will change it after – x86bit-destroyer Oct 27 '14 at 17:54
  • Wrap your view in pre tag
     $output 
    – morissette Oct 27 '14 at 17:56

1 Answers1

0

As @itachi mentioned, nl2br should solve this problem.

Brian Anderson
  • 621
  • 7
  • 22