简体   繁体   English

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

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

I am getting the following error while trying to insert data into database.尝试将数据插入数据库时​​出现以下错误。

Error:错误:

#1064 - You have an error in your SQL syntax; #1064 - 你的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Live Your Life Well'.检查与您的 MySQL 服务器版本相对应的手册,了解在“好好生活”附近使用的正确语法。 Live Your Life Well' was a theme designed to encourage peo' at line 1 Live Your Life Well' 是一个主题,旨在鼓励人们在第 1 行

I am providing the query below:我提供以下查询:

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'

How can I resolve this error?我该如何解决这个错误?

if insert then如果插入然后

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

if update then如果更新然后

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

but following SQL is also executing fine.但是下面的 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'

this is exact query i tested by myself and it is executing fine.这是我自己测试的确切查询,并且执行良好。

You are passing description '$newCustomerobj->description' with single quotes.您正在使用单引号传递 description '$newCustomerobj->description' You first add backslash like:您首先添加反斜杠,如:

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

Now pass this variable in your query.现在在您的查询中传递这个变量。

The problem is with the quotes.问题在于引号。 You should escape the special characters using the \\ character.您应该使用\\字符转义特殊字符。 Try with,试试看,

 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'

The following code should work, you'll need backslashes for quotes in your string.以下代码应该可以工作,您需要在字符串中使用backslashes来表示引号。 I've reformatted your code, this should now work.我已经重新格式化了你的代码,现在应该可以工作了。

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'

Hope this helps, thanks!希望这有帮助,谢谢!

Its because of single quotes.这是因为单引号。

In MySQL, strings (field values) are enclosed with single quotes.在 MySQL 中,字符串(字段值)用单引号括起来。

So, if single quote comes in the string itself, string breaks and upcoming words are considered as MySQL Reserved Keywords .因此,如果字符串本身出现单引号,则字符串中断和即将出现的单词将被视为MySQL Reserved Keywords

Use mysqli_real_escape_string()使用mysqli_real_escape_string()

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

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