【问题标题】:Getting SQL error while inserting the data into database using MySQL and PHP使用 MySQL 和 PHP 将数据插入数据库时​​出现 SQL 错误
【发布时间】:2016-02-12 10:10:43
【问题描述】:

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

错误:

#1064 - 您的 SQL 语法有错误;查看与您的 MySQL 服务器版本相对应的手册,以了解在“过好你的生活”附近使用的正确语法。 Live Your Life Well' 是一个主题,旨在鼓励第 1 行的 peo'

我提供以下查询:

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'

我该如何解决这个错误?

【问题讨论】:

标签: php mysql


【解决方案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'

这是我自己测试的确切查询,它执行得很好。

【讨论】:

  • @ Tahir :您删除的答案实际上是我想要的完美。
  • 我通过这种方式使用INSERT INTO table_name("column_names_separates_by_commas") VALUES ("values_for_columns_separated_by_commas"); 解决了这个问题。
  • 很高兴我能帮上忙,快乐编码:)
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    下面的代码应该可以工作,你需要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'
    

    希望对你有帮助,谢谢!

    【讨论】:

    • 我正在为字段description传递一个段落。这是我的实际查询$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')."' ";
    【解决方案4】:

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

     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】:

      这是因为单引号。

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

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

      使用mysqli_real_escape_string()

      【讨论】:

        猜你喜欢
        • 2011-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多