字符串拼接讲解

ajax易错点,排查json,sql语句,字符串拼接错误

1.字符串拼接
在写json类型数据时,经常会进行字符串拼接,常常让新人拼的一脸懵比,其实掌握规律很简单:
比如下面sql语句:

//原sql语句:
//sql1="update news set newsTitle="title",newsContent="content"  where newsID="nid";

sql1="update news set newsTitle=\""+title+"\",newsContent=\""+content+"\"  where newsID=\""+nid+"\"";

1.先把sql=" 内容"理解一下,数据库接受的是字符串,内容每次都不一样,但最外层引号不变。
2.要传值进入sql的话,先写出正常的sql语句

sql1="update news set newsTitle="title",newsContent="content"  where newsID="nid";

比如前台传过来title,content,nid的值,想要改变它们,

原sql 加斜杠转义
newsTitle=“title” newsTitle=\“title\”
加斜杠转义 json支持
newsTitle=“title” newsTitle=\"“+title+”\"

就是先转义,再加引号加号就OK

猜你喜欢

转载自blog.csdn.net/code_mzh/article/details/105505698