簡體   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