2012-03-30 147 views
-1

我这样做插入查询:SQL语法错误?

INSERT INTO `giornale`.`Articolo` 
    (`id_articolo`, `titolo`, `didascalia`, `testo`, `immagine`, 
     `username_r`, `data_art`, `autore`, `id_categoria`, `numero_like`) 
VALUES (NULL, \'titoloooo\', \'ciao ciao\', \'texttttt\', \'immag\', 
      \'admin\', NOW(), \'prova\', \'3\', NULL); 

,如果我在phpMyAdmin SQL编辑器中启动它,它工作正常。但如果我在我的PHP脚本中这样做,它返回我这个错误:

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 '\'titoloooo\', \'ciao ciao\', \'texttttt\', \'immag\', \'admin\', NOW(), \'prova' at line 1

我该如何解决这个问题?

非常感谢!

+7

看起来你正在逃避你需要的引号。 – Wiseguy 2012-03-30 15:30:43

+3

你能告诉我们PHP代码吗?你真的逃避你的SQL编辑器中的单引号吗?肯定会失败。 – MichaelRushton 2012-03-30 15:31:45

回答

4

我不知道为什么你有所有这些反斜杠。

删除他们,你应该没问题。

INSERT INTO `giornale`.`Articolo` 
    (`id_articolo`, `titolo`, `didascalia`, `testo`, `immagine`, 
     `username_r`, `data_art`, `autore`, `id_categoria`, `numero_like`) 
VALUES (NULL, 'titoloooo', 'ciao ciao', 'texttttt', 'immag', 
      'admin', NOW(), 'prova', '3', NULL); 
+0

除非他使用'mysql_query('INSERT INTO ...' – MichaelRushton 2012-03-30 15:33:46

+0

@MichaelRushton那么逃跑的版本应该有**失败**在phpmyadmin – Neal 2012-03-30 15:34:40

+0

是的,我提到,作为他的问题的评论 – MichaelRushton 2012-03-30 15:35:59

1

你不需要逃脱单引号

INSERT INTO `giornale`.`Articolo` 
    (`id_articolo`, `titolo`, `didascalia`, `testo`, `immagine`, 
     `username_r`, `data_art`, `autore`, `id_categoria`, `numero_like`) 
VALUES (NULL, 'titoloooo', 'ciao ciao', 'texttttt', 'immag', 
      'admin', NOW(), 'prova', '3', NULL); 
+0

除非他使用'mysql_query('INSERT INTO ...' – MichaelRushton 2012-03-30 15:34:13

+0

这将假设,我只是在我的答案时正在处理问题。 – ncremins 2012-03-30 16:06:52

1

使用的字符串双引号,那么你可以使用单引号,而不在您的查询的问题:

$query = "INSERT INTO `giornale`.`Articolo` (`id_articolo`, `titolo`, `didascalia`, `testo`, `immagine`, `username_r`, `data_art`, `autore`, `id_categoria`, `numero_like`) VALUES (NULL, 'titoloooo', 'ciao ciao', 'texttttt', 'immag', 'admin', NOW(), 'prova', '3', NULL);"; 
mysql_query($query); // or whatever 
0

它在我看来,\'正在被带到您的SQL查询。尝试删除转义并单独使用单引号。

INSERT INTO `giornale`.`Articolo` 
    (`id_articolo`, `titolo`, `didascalia`, `testo`, `immagine`, 
     `username_r`, `data_art`, `autore`, `id_categoria`, `numero_like`) 
VALUES (NULL, 'titoloooo', 'ciao ciao', 'texttttt', 'immag', 
      'admin', NOW(), 'prova', '3', NULL); 
0

如果整个字符串都用单引号',它会正常工作。

它看起来像你使用双引号,在这种情况下,你不必逃避单引号。