Saving text area into mySQL database field PHP

Saving text area into mySQL database field PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Saving Text Area Content into MySQL: Formatting, Security, and Modern Practices

You are facing a classic web development scenario where client-side formatting needs to survive server-side storage. Based on your code snippet, you can connect to the database successfully, but the data stored in player_data is empty or stripped of formatting. This usually stems from three distinct issues: how the form submits data, how the database column is defined, and critical security vulnerabilities in your current implementation.

First, let's address the most immediate blocker. Your HTML form uses method="post", yet your PHP script retrieves the value using $_GET['Comments']. The HTTP protocol strictly separates GET requests (which append data to the URL) from POST requests (which send data in the body). Because your form is posting, $_GET will never contain the content of the text area. You must switch that retrieval line to:

$text = $_POST['Comments'];

However, even if you fix the variable access, your current code will still fail to preserve rich formatting like bold text or lists. This is almost certainly because your database column player_data is defined as a standard VARCHAR. A VARCHAR field has a fixed character limit (e.g., 255 characters). OpenWYSIWYG generates HTML tags (<p>, <b>, <div>), which are significantly longer than plain text. Once the string exceeds the column's length, MySQL truncates the data silently.

To store formatted content, you must define the database column as TEXT or MEDIUMTEXT. If you alter an existing table, run:

ALTER TABLE KeepData MODIFY COLUMN player_data TEXT;

Beyond storage mechanics, your current approach uses the deprecated mysql_* extension and performs direct string interpolation for SQL queries. This is a severe security risk known as SQL Injection. An attacker could modify the input field to include characters like ' OR '1'='1, allowing them to access or delete your entire database.

The correct modern approach involves using prepared statements with PDO or MySQLi. Here is how you should refactor your PHP logic to handle $_POST data securely while preserving formatting:

<?php
// Establish connection using PDO (preferred over mysql_* extension)
$dsn = "mysql:host=localhost;dbname=databasename;charset=utf8";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

try {
    $pdo = new PDO($dsn, "user", "password", $options);
    
    // Prepare the statement with a placeholder
    $stmt = $pdo->prepare("INSERT INTO KeepData (player_data) VALUES (:text)");
    
    // Bind and execute. The driver handles quoting safely.
    $stmt->execute([
        ':text' => $_POST['Comments'] ?? '' 
    ]);
    
} catch (PDOException $e) {
    die("Database error: " . htmlspecialchars($e->getMessage()));
}
?>

There is also a crucial consideration regarding the content itself. When users paste text or use WYSIWYG editors, they often introduce unwanted whitespace, carriage returns (\r), or zero-width characters that can break layouts or cause storage bloat. Depending on your requirements, you might want to strip HTML tags entirely if you only need plain text, or sanitize the HTML using a library like HTMLPurifier if you intend to render the content back to the user later.

If you are building this application with Laravel, the framework's Eloquent ORM handles these details for you. You would define the migration with a text column and use a model like:

$article = Article::create([
    'player_data' => $_POST['Comments'] ?? ''
]);

Laravel automatically binds parameters to prevent SQL injection and manages character sets efficiently. For further details on handling text areas in modern frameworks, check out resources from LaravelCompany, which often covers these specific implementation patterns.

Our network