简体   繁体   中英

PHP MySQL query problem with string despite using mysql_real_escape_string

The following SQL query:

INSERT INTO `database`.`table` (`param1`, `param2`) 
VALUES (
'Новости Томска – подборка новостей из общественной жизни города, политики, спорта, обзор происшествий, событий.\r\nПолезная информация о недвижимости, авто, финансах, работе и консультации специалистов.\r\nОбъявления по различным тематикам, вакансии томских работодателей. \', 
'Томский городской портал, Томск, Портал города Томска, Недвижимость в Томске, Авторынок Томска, Продажа авто в Томске, Работа в Томске, Вакансии, Резюме, Отдых в Томске, Афиша Томска, Новости Томска, Томский форум, Погода в Томске, Томские сайты, Каталог томских сайтов, Частны');

failed with error message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Томский городской портал, Томск, Портал гор' at line 1

I have used mysql_real_escape_string on the strings before using them on the SQL query, so I thought this was enough to make them error-free.

My database is set to use

utf8_general_ci

as Collation for these fields.

What can be the problem with the query?

You are escaping the ' that should close the value of param1 (the first value in the values clause) :

INSERT INTO `database`.`table` (`param1`, `param2`) 
VALUES (
'...й. \',   <= there
'...ны');


You should remove the \\ before the closing ' , so your query looks like this :

INSERT INTO `database`.`table` (`param1`, `param2`) 
VALUES (
'...й. ', 
'...ны');


The \\ is necessary to escape quotes inside the strings -- and not quotes that are string delimiters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM