3
insert into sent_message (user_id,subject,message,background_url,total_recipients,created_at) values ('115','Greeting','Hx z'xi','/images/default/1.jpg',2,'2015-07-23 10:48:41')

For the message column i have used the value with single quotes hence i got the insertion error....

And i tried to change the query as below but the output and the error is same. Ho to over come it.

SET QUOTED_IDENTIFIER ON GO insert into sent_message (user_id,subject,message,background_url,total_recipients,created_at) values ('115','Greeting','Hx z'xi','/images/default/1.jpg',2,'2015-07-23 10:48:41')
4

2 回答 2

3

您是否尝试过使用准备好的语句?这样您就不必担心在值部分添加引号;您使用问号作为占位符。

addslashes然后,当您将变量/字符串传递给执行方法时,您可以使用转义任何双引号/单引号。

你没有提到你正在使用哪个数据库,所以我假设 MySQL?

这是关于准备好的语句的 PHP 手册:

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

于 2015-07-23T09:25:15.343 回答
0

单引号 (') 的转义字符是两个单引号 ('')。因此示例:

Insert into SomeTable(TextField) Select 'Some ''single quotes inserted'''

将产生:

Select TextField from SomeTable

Some 'single quotes inserted'
于 2015-07-23T11:43:08.373 回答