使用MySQL和PHP将数据插入数据库时​​获取与sql相关的错误

时间:2016-02-12 10:10:44

标签: php mysql

尝试将数据插入数据库时​​出现以下错误。

  

错误:

#1064 - 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 'Live Your Life Well'. Live Your Life Well' was a theme designed to encourage peo' at line 1

我正在解释下面的查询。

INSERT phr_health_care set title='Mental Health Awareness',description='In 2010, the theme was 'Live Your Life Well'. Live Your Life Well' was a theme designed to encourage people to take responsibility for the prevention of mental health issues during times of personal challenge and stress. The message was to inform the public that many mental health problems could be avoided by striving toward and making positive lifestyle choices in the ways we act and think',status='1',image='4o2kgkpgu_awarenessdaycolorgen.jpg',date='2016-02-12 15:32:44'

请帮我解决此错误。

5 个答案:

答案 0 :(得分:1)

如果插入则

INSERT INTO table_name("column_names_separates_by_commas") VALUES ("values_for_columns_separated_by_commas");

如果更新则

UPDATE table_name SET column1="value1", column2="vaule2", columnN="valueN"

但是后面的SQL也正常运行。

INSERT phr_health_care set 
title='Mental Health Awareness',
description="In 2010, the theme was 'Live Your Life Well'. Live Your Life Well' was a theme designed to encourage people to take responsibility for the prevention of mental health issues during times of personal challenge and stress. The message was to inform the public that many mental health problems could be avoided by striving toward and making positive lifestyle choices in the ways we act and think",
status='1',
image='4o2kgkpgu_awarenessdaycolorgen.jpg',
date='2016-02-12 15:32:44'

这是我自己测试的确切查询,并且执行正常。

答案 1 :(得分:1)

您正在使用单引号传递说明'$newCustomerobj->description'。您首先添加反斜杠,如:

$description = addslashes($newCustomerobj->description);

现在在您的查询中传递此变量。

答案 2 :(得分:0)

问题在于引号。您应该使用\字符转义特殊字符。试试,

 INSERT phr_health_care set title='Mental Health 
Awareness',description='In 2010, the theme was \'Live Your Life Well\'. 
Live Your Life Well\' was a theme designed to encourage people to take 
responsibility for the prevention of mental health issues during times of
personal challenge and stress. The message was to inform the public that
 many mental health problems could be avoided by striving toward and 
making positive lifestyle choices in the ways we act and 
think',status='1',image='4o2kgkpgu_awarenessdaycolorgen.jpg',date='2016-
02-12 15:32:44'

答案 3 :(得分:0)

以下代码应该有效,您需要backslashes来表示字符串中的引号。我已经重新格式化了你的代码,现在应该可以了。

INSERT `phr_health_care` SET `title` = 'Mental Health Awareness', 
`description` = 'In 2010, the theme was \'Live Your Life Well\'. Live Your 
Life Well' was a theme designed to encourage people to take responsibility for
 the prevention of mental health issues during times of personal challenge and 
stress. The message was to inform the public that many mental health problems 
could be avoided by striving toward and making positive lifestyle choices in 
the ways we act and think', `status` = '1', `image` = 
'4o2kgkpgu_awarenessdaycolorgen.jpg', `date` = '2016-02-12 15:32:44'

希望这有帮助,谢谢!

答案 4 :(得分:0)

因为单引号。

在MySQL中,字符串(字段值)用单引号括起来。

因此,如果单引号出现在字符串本身中,则字符串中断和即将出现的单词将被视为MySQL Reserved Keywords

使用mysqli_real_escape_string()