繁体   English   中英

使用 MySQL 和 PHP 将数据插入数据库时​​出现 SQL 错误

[英]Getting SQL error while inserting the data into database using MySQL and PHP

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

错误:

#1064 - 你的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,了解在“好好生活”附近使用的正确语法。 Live Your Life Well' 是一个主题,旨在鼓励人们在第 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'

我该如何解决这个错误?

如果插入然后

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'

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

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

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

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

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

 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'

以下代码应该可以工作,您需要在字符串中使用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'

希望这有帮助,谢谢!

这是因为单引号。

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

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

使用mysqli_real_escape_string()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM