Create a field with more than 1000 characters (with html tags in it, for example <b>), then try to change it (with pencil icon) and add one more character. You will see that in SQL-query field at the top appears html not encoded symbols.
$query_base = htmlspecialchars($sql_query); // htmlspecialchars added.
I'm not sure we have to add this function here. Think you see the problem and will solve it perfectly.
Second problem: On the page there is another SQL-query that shows data in the table, because of define mechanism used
we have [...] ending in that query too. I can't help you to solve problem with this ending, think we have to change define mechanism there or use it in another way.