0

I am getting the following error while trying to insert data into database.

Error:

#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

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?

Community
  • 1
  • 1
satya
  • 3,508
  • 11
  • 50
  • 130
  • You need to escape the single quotes in tour query. – daker Feb 12 '16 at 10:14
  • Please put whitespaces after comma's and [Please read this excellent post on Stackoverflow, on when to use single and double quotes and backticks](http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks/11321508#11321508) – Werner Feb 12 '16 at 10:27

5 Answers5

1

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.

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.

Muhammad Husnain Tahir
  • 1,009
  • 1
  • 15
  • 28
1

You are passing description '$newCustomerobj->description' with single quotes. You first add backslash like:

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

Now pass this variable in your query.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
0

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'
Jenz
  • 8,280
  • 7
  • 44
  • 77
0

The following code should work, you'll need backslashes for quotes in your string. 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!

Panda
  • 6,955
  • 6
  • 40
  • 55
  • i am passing one one paragraph for the field `description`.here is my actual query `$sql="INSERT ".PREFIX."health_care set title='".$newCustomerobj->title."',description='".$newCustomerobj->description."',status='".$newCustomerobj->status."',image='".$newCustomerobj->image."',date='".date('Y-m-d H:i:s')."' ";` – satya Feb 12 '16 at 10:17
0

Its because of single quotes.

In MySQL, strings (field values) are enclosed with single quotes.

So, if single quote comes in the string itself, string breaks and upcoming words are considered as MySQL Reserved Keywords.

Use mysqli_real_escape_string()

Pupil
  • 23,834
  • 6
  • 44
  • 66