How to deal with Apostrophe while writing into Mysql database
我得到这个错误:
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 's','portal','','offering','MSNBC','News','','sports','','MSN','Money','','games'' at line 3
唯一的问题是,插入包含撇号的数据时会出现此错误。我尝试将数据类型从
我试着把
如何解决这个问题?
编辑:
1 2 3 4 5 | $query=" INSERT INTO alltags (id,tag1,tag2,tag3,tag4,tag5,tag6,tag7,tag8,tag9,tag10,tag11,tag12,tag13,tag14,tag15,tag16,tag17,tag18,tag19,tag20,tag21,tag22,tag23,tag24,tag25,tag26,tag27,tag28,tag29,tag30) VALUES ('',mysql_real_escape_string($uniqkey[0]),mysql_real_escape_string($uniqkey[1]),mysql_real_escape_string($uniqkey[2]),mysql_real_escape_string($uniqkey[3]),mysql_real_escape_string($uniqkey[4]),mysql_real_escape_string($uniqkey[5]),mysql_real_escape_string($uniqkey[6]),mysql_real_escape_string($uniqkey[7]),mysql_real_escape_string($uniqkey[8]),mysql_real_escape_string($uniqkey[9]),mysql_real_escape_string($uniqkey[10]),mysql_real_escape_string($uniqkey[11]),mysql_real_escape_string($uniqkey[12]),mysql_real_escape_string($uniqkey[13]),mysql_real_escape_string($uniqkey[14]),mysql_real_escape_string($uniqkey[15]),mysql_real_escape_string($uniqkey[16]),mysql_real_escape_string($uniqkey[17]),mysql_real_escape_string($uniqkey[18]),mysql_real_escape_string($uniqkey[19]),mysql_real_escape_string($uniqkey[20]),mysql_real_escape_string($uniqkey[21]),mysql_real_escape_string($uniqkey[22]),mysql_real_escape_string($uniqkey[23]),mysql_real_escape_string($uniqkey[24]),mysql_real_escape_string($uniqkey[25]),mysql_real_escape_string($uniqkey[26]),mysql_real_escape_string($uniqkey[27]),mysql_real_escape_string($uniqkey[28]),mysql_real_escape_string($uniqkey[29]))"; mysql_query($query) or die(mysql_error()); |
我把它改成了
包含MySQL可能解释的字符的数据编码过程称为"转义"。必须使用
在构建查询之前,必须先转义数据。此外,您还可以使用php的循环结构和
1 2 3 4 5 6 7 8 9 10 11 12 | // Build tag fields $tags = 'tag' . implode(', tag', range(1,30)); // Escape each value in the uniqkey array $values = array_map('mysql_real_escape_string', $uniqkey); // implode values with quotes and commas $values ="'" . implode("', '", $values) ."'"; $query ="INSERT INTO alltags (id, $tags) VALUES ('', $values)"; mysql_query($query) or die(mysql_error()); |
使用mysql_real_escape_string是处理SQL插入/更新字符的一种更安全的方法:
1 2 3 4 |
另外,我将把您的列从文本改回varchar-搜索,除了索引之外,工作得更好。
更新您的更新由于
将其从列列表中删除,这样就不必在values子句中提供值:
在列列表中包含
id ,这要求在values子句中使用其中一个值:NULL DEFAULT
下面是一个使用空值作为ID占位符的示例:
我想强调的是,您不应该这样设置列。
美加的回答略有改进:
编辑:美加更新了他的帖子,所以他的回答现在更好了。
1 2 3 4 5 6 7 8 9 10 11 12 13 | $query = 'INSERT INTO alltags (id, '; // append tag1, tag2, etc. $query .= 'tag' . implode(', tag', range(1, 30)) .") VALUES ('',"; // escape each value in the uniqkey array $escaped_tags = array_map('mysql_real_escape_string', $uniqkey); // implode values with quotes and commas, and add closing bracket $query .="'" . implode("', '", $escaped_tags) ."')"; // actually query mysql_query($query) or die(mysql_error()); |
请看我的回答。这是正确的代码。
如果要使用错误引导的mysql_query()函数,则必须按如下方式分解SQL字符串:
1 2 3 4 5 6 7 8 9 10 11 | mysql_query( "INSERT INTO whateever (col1,col2,col3,col4) VALUES (" . mysql_real_escape_string($col1) ."," . mysql_real_escape_string($col2) ."," . mysql_real_escape_string($col3) ."," . mysql_real_escape_string($col4) .")" ); |
或者,由于您有一个数组,请使用巧妙的方法调用立即全部转义:
1 2 3 | $uniqkey = array_map("mysql_real_escape_string", $uniqkey); mysql_query("USE THE ESCAPED ARRAY THEN DIRECTLY ('$uniqkey[0]', '$uniqkey[1]', '$uniqkey[2]', '$uniqkey[3]', ..."); |