要注意两点:

1、在SQL语句中,如果要存入单引号,应该使用两个单引号来代替一个单引号

如:

insert into article(title,category,content,author_acc,author_name,create_time,last_edit_time)
values('test3','sg',
'<p><span style=''font-weight: bold;''>sdg<span style=''font-style: italic;''>sg</span></span><span style=''font-size: 1rem; font-style: italic;''></span><br></p>','123','admin','2020/4/22','2020/4/22')

2、首先将要存储字符串汇中的所有单引号(’)替换为单引号(’)来存储,这样才能通过sql语句存入数据库中,这里在前端可以使用js的替换某字符串中所有该字符的方法:

/创建replaceAll()函数
            String.prototype.replaceAll = function (FindText, RepText) {
                return this.replace(new RegExp(FindText, "g"), RepText);
            };

//使用方法,注意替换引号时要使用转义符
            var testStr="121'212";
            testStr.replaceAll("1","3");//将1替换为3
            testStr.replaceAll("\'","\'\'");//将单引号替换为两个单引号,这里要使用转义符

3、首先最好将html代码中的所有双引号(")换为单引号(’)来存储,方便数据的封装和传递。这里在前端可以使用js的替换某字符串中所有该字符的方法

/创建replaceAll()函数
            String.prototype.replaceAll = function (FindText, RepText) {
                return this.replace(new RegExp(FindText, "g"), RepText);
            };
//使用方法,注意替换引号时要使用转义符
            var testStr="121212";
            testStr.replaceAll("1","3")

更多推荐

sql——如何将html代码存入数据库中