Answer the question
In order to leave comments, you need to log in
How to correctly escape characters in SQL queries if you need to send HTML code?
I interpose (INSERT) the data into a DB through PHP.
The $FnlCodeMsgMobile tag data is not inserted.
$FnlCodeMsgMobile = "
<img src=\"$NewFileDir\" alt=\"$NewFileDir\" class='responsive-img' id='dynamic$maxIDNumberMobile'>
<script>
document.getElementById('$maxIDNumberMobile').addEventListener('click', function() {
lightGallery(document.getElementById('$maxIDNumberMobile'), {
dynamic: true,
thumbnail: true,
dynamicEl: [{
"src": '$NewFileDir',
}]
})
});
</script>
";
Answer the question
In order to leave comments, you need to log in
HTML does not need to be specially escaped. And you don't need to screen anything at all . Shielding is trash, waste, the last century and injections. The data in a DB should be sent simply separately from request.
It's amazing that so many people signed up for this question.
Well, I don’t know - how to subscribe to the question "how much is six six?". Not "twice two", but close.
Working with the database is the very basics of PHP, which are covered in every textbook from all sides. Is it really so bad that this question is of such great interest to be on the list of interesting ones on the toaster?
And your personal problem here is not at all "how to screen". HTML does not need to be escaped in any special way, the database absolutely does not care what you insert into it. Your problem is that you basically don't know PHP and get confused in the basics of the language, you use an outdated version that will not work on a live site. And even the most basic syntax scares you. Therefore, I'm sorry, I will write an answer not for you, but for those who, once again, ask this evergreen question, but for some reason could not master the basic skills of working with Google / Yandex.
To insert data into mysql, and never think about any "escaping" at all, you need to
1. Replace all variables in the query with question marks
2. Prepare the query for execution
3.
4. Run the request.
As I said, the functions that begin with mysql are long gone in the language. Only those that start with mysqli and PDO remain. Here I will give examples for them.
mysqli
$type = 'testing';
$reporter = "John O'Hara";
$query = "INSERT INTO contents (type, reporter) VALUES(?, ?)"; // заменем
$stmt = $mysqli->prepare($query); // подготавливаем
$stmt->bind_param("ss", $type, $reporter); // привязываем
$stmt->execute(); // исполняем
$type = 'testing';
$reporter = "John O'Hara";
$query = "INSERT INTO contents (type, reporter) VALUES(?, ?)"; // заменяем
$stmt = $pdo->prepare($query); // подготавливаем
$stmt->execute([$type, $reporter]); // привязываем и исполняем
I love using the safemysql library
https://github.com/colshrapnel/safemysql
phpfaq.ru/safemysql
In short:
$data = array( 'field' => $FnlCodeMsgMobile );
$db->query( 'INSERT INTO ?n SET ?u', 'table_name', $data );
$data =
<<< start
<img src=\"$NewFileDir\" alt=\"$NewFileDir\" class='responsive-img' id='dynamic$maxIDNumberMobile'>
<script>
document.getElementById('$maxIDNumberMobile').addEventListener('click', function() {
lightGallery(document.getElementById('$maxIDNumberMobile'), {
dynamic: true,
thumbnail: true,
dynamicEl: [{
"src": '$NewFileDir',
}]
})
});
</script>
start;
Nikita Guriev , do
$sql = "INSERT INTO `ваша_таблица` data='".mysql_real_escape_string($data)."' WHERE ваше условие вставки";
"src": '$NewFileDir',
$sql = "INSERT INTO `ваша_таблица` data='".base64_encode( gzcompress($data) )."'";
$data = gzuncompress( base64_decode( $row['data' ] ) );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question