Unable to save large text in mysql thru php

2019-06-24 15:13发布

问题:

$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.

回答1:

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

Use this query, and modify the database field's property to BLOB text using PHPmyAdmin.

I works for me when i need to insert large contents.



回答2:

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.



回答3:

To insert data in a Database always use a PreparedStatement! Never trust the users input!



回答4:

I suggest you use mysqli_stmt::send_long_data : http://php.net/manual/en/mysqli-stmt.send-long-data.php . There are some good examples in manual/comments.