Unable to save large text in mysql thru php

draxxxeus picture draxxxeus · May 30, 2011 · Viewed 9.7k times · Source
$query="INSERT INTO `ARTICLES` (`TITLE`, `BY`, `IN`, `POST`) 
VALUES('". $title ."', '". $by ."', '". $in ."', '". $_POST['post'] ."')";

This code is able to save small length text but not large. The datatype for the POST field is longtext. Also if i insert data thru phpmyadmin, it gets saved.

Answer

Christophe picture Christophe · May 30, 2011

like Felix kling says you need to escape your post data because maybe there are some quotes in the text you try to save but it will prevent your query to run properly and it is also a major security risk not to escape before sending to the database.

$post = mysql_real_escape_string($_POST['post']);

$query="INSERT INTO `ARTICLES` (`TITLE`, `BY`, `IN`, `POST`) 
VALUES('". $title ."', '". $by ."', '". $in ."', '". $post ."')";

also make sure you've set the POST column to text in phpmyadmin. Because if you didn't prepare enough space it won't save to the database.