使用MySQL 5数据库,我尝试使用内联HTML手动更新数据库中的字段:
UPDATE employee SET bio = "<div style="text-align:center"> <H2>Employment History</H2> <table class='table' style="width:auto;margin-left:auto;margin-right:auto;"><tr><th>Year</th><th>Company</th></tr><tr><td>2013-14</td><td>IBM</td></tr><tr><td>2015-16</td><td>Microsoft</td></tr><tr><td>2016-17</td><td>Google</td></tr></table><H2>BIO</H2></div><p>John Smith started out as developer and became project manager.</p>" where ID = 100;
(顺便说一下,这不是一行 - 当我在MySQL Shell中复制/粘贴它时,它会被包装成多行)
导致以下错误:
ERROR 1064 (42000): 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 'margin-left:auto' at line 1
ERROR 1064 (42000): 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 'margin-right:auto' at line 1
我可能做错了什么?有没有办法在UPDATE或ALTER SQL语句中使用HTML文件进行此更改?
答案 0 :(得分:2)
您在字符串中使用相同的引号,因为您正在使用字符串中的HTML属性。您需要使用不同的引号或转义内部引号。
UPDATE employee SET bio = "<div style='text-align:center'> <H2>Employment History</H2> <table class='table' style='width:auto;margin-left:auto;margin-right:auto;'><tr><th>Year</th><th>Company</th></tr><tr><td>2013-14</td><td>IBM</td></tr><tr><td>2015-16</td><td>Microsoft</td></tr><tr><td>2016-17</td><td>Google</td></tr></table><H2>BIO</H2></div><p>John Smith started out as developer and became project manager.</p>" where ID = 100;
如果您是从客户端应用程序语言执行此操作,请使用预准备语句,而不是直接替换为SQL字符串。