鍍金池/ 問答/數(shù)據(jù)庫  網(wǎng)絡安全  HTML/ 請教:node.js express mysql存儲文章內容(html代碼)代碼

請教:node.js express mysql存儲文章內容(html代碼)代碼報錯

**環(huán)境:
node.js express mysql**
問題:
mysql 插入文章數(shù)據(jù)時,存儲文章詳情內容時報錯,如下:
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 'http://example.com/movies.json')
普通文本內容沒問題,但里面出現(xiàn)"代碼片段"好像就報錯。
(是不是得怎么轉譯一下,求方法,剛接觸node.js)
語法這么寫得:

insert into article_list
          (title,author,summary,is_top,catalog_alias,content)
           values
            ('${req.body.title}',
            '${req.body.author}',
            '${req.body.summary}',
            '${req.body.is_top}',
            '${req.body.catalog_alias}',
            '${req.body.content}')`;

如圖所示:
圖片描述

回答
編輯回答
任她鬧

不要直接拼接字符串,如果你的內容包括以下符號 ' -- [ ] 等 ,可能會報錯,
例如本來你想查詢 select * from table where name = '${var1}';, 如果你的變量var1 是 Mr.W'O,那么拼接后你的語句就變成了 select * from table where name = 'Mr.W'O'; 語句中出現(xiàn)了三個 ' 當然會報語法錯誤。 報錯還是小事情,被黑客利用而進行sql注入就麻煩了。
建議你用node的模塊mysql或mysql2 對變量進行編碼,可能你的寫法就變成了這樣

var sql = `insert into article_list
          (title,author,summary,is_top,catalog_alias,content)
           values
            (?,?,?,?,?,?)`;
let result = mysql.query(sql, [req.body.title, req.body.author,req.body.summary,req.body.is_top])
2017年1月6日 00:12