【问题标题】:Is there a problem with this MySQL Query?这个 MySQL 查询有问题吗?
【发布时间】:2010-05-10 18:04:44
【问题描述】:
public function create() {


        echo $this->equipment->getCatId() . "<br/>";
        echo $this->equipment->getName() . "<br/>";
        echo $this->equipment->getYear() . "<br/>";
        echo $this->equipment->getManufacturer() . "<br/>";
        echo $this->equipment->getModel() . "<br/>";
        echo $this->equipment->getPrice() . "<br/>";
        echo $this->equipment->getLocation() . "<br/>";
        echo $this->equipment->getCondition() . "<br/>";
        echo $this->equipment->getStockNum() . "<br/>";
        echo $this->equipment->getInformation() . "<br/>";
        echo $this->equipment->getDescription() . "<br/><br/>";


        $db = Connect::connect();
        $current_time = date('y M d');
        $query = "INSERT INTO equipment (cat_id, name, year, manufacturer, model, price, location, condition,
                                         stock_num, information, description, created, modified)

                                         VALUES

                                        ({$this->equipment->getCatId()}, {$this->equipment->getName()}, {$this->equipment->getYear()},
                                         {$this->equipment->getManufacturer()}, {$this->equipment->getModel()}, {$this->equipment->getPrice()},
                                         {$this->equipment->getLocation()}, {$this->equipment->getCondition()}, {$this->equipment->getStockNum()},
                                         {$this->equipment->getInformation()}, {$this->equipment->getDescription()}, '$current_time', '$current_time')";

        $result = $db->query($query);

        return $db->insert_id;

    }
  • 顶部的回显都显示符合数据库架构的有效数据。
  • 没有连接错误

有什么想法吗?

提前致谢!

这是回显的查询

INSERT INTO 设备(cat_id、名称、年份、制造商、型号、价格、位置、状况、stock_num、信息、描述、创建、修改)值(1、'r'、1、'sdf'、'sdf' , '2', 'd', 'd', '3', 'asdfasdfdf', 'df', '10 年 5 月 10 日', '10 年 5 月 10 日')

MySQL 给出:#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法,以便在 'condition, stock_num, information, description, created, modified) VALUES (1, 'r' at line 1

id int(11) unsigned NO PRI NULL auto_increment 编辑 删除 cat_id int(11) unsigned NO NULL
编辑 删除 prod_name varchar(255) YES NULL
编辑 删除 prod_year varchar(10) YES NULL
编辑 删除制造商 varchar(255) YES NULL
编辑删除模型 varchar(255) YES NULL
编辑 删除价格 varchar(10) YES NULL
编辑删除位置 varchar(255) YES NULL
编辑删除条件 varchar(25) YES NULL
编辑 删除 stock_num varchar(128) YES NULL
编辑 删除信息文本 YES NULL
编辑 删除描述文本 YES NULL
Edit Delete created varchar(20) YES NULL
Edit Delete 修改的 varchar(20) YES NULL

查询:INSERT INTO 设备(cat_id、prod_name、prod_year、制造商、型号、价格、位置、条件、stock_num、信息、描述、创建、修改)VALUES(1、'asdf'、'234'、'adf' , 'asdf', '34', 'asdf', 'asdf', '234', 'asdf', 'asdf', '10 年 5 月 10 日', '10 年 5 月 10 日')

这是从 PhpMyAdmin 导出的 SQL,以防有人想尝试复制此问题:http://pastie.org/954206

BLEHBLEHSDFOHSE -- 很明显,“条件”也是一个保留字……加上一些反引号,然后它就开始工作了。

【问题讨论】:

  • 也不知道那个.. 不错。 :-)
  • 语法错误发生在 MySQL 接触数据库之前,因此它们不能与单个查询中不存在的任何内容(例如列类型)相关联。语法错误的错误消息非常无用(尽管引用的字符串确实从错误的确切点开始);使用可以突出语法的东西(例如 MySQL 查询浏览器)有助于避免很多基本错误,而不会浪费太多时间。
  • SQL/PHP 的混合很可怕,你需要改用 MySQL 绑定语句。

标签: php mysql insert object


【解决方案1】:

YEAR 是 MySQL 中的保留字。如果要使用它,则需要反引号(即`year`

【讨论】:

    【解决方案2】:

    假设位置、信息和描述是字符串,缺少引号。

    INSERT INTO equipment (cat_id, name, year, manufacturer, model, price, location, condition,
                                             stock_num, information, description, created, modified)
    
                                             VALUES
    
                                            ({$this->equipment->getCatId()}, {$this->equipment->getName()}, {$this->equipment->getYear()},
                                             '{$this->equipment->getManufacturer()}', {$this->equipment->getModel()}, {$this->equipment->getPrice()},
                                             '{$this->equipment->getLocation()}', {$this->equipment->getCondition()}, {$this->equipment->getStockNum()},
                                             '{$this->equipment->getInformation()}', '{$this->equipment->getDescription()}', '$current_time', '$current_time')";
    

    【讨论】:

    • 如何将引号应用于返回字符串的方法?你所说的字符串,是指文本吗?
    • 按照示例执行 '{$this->equipment->getLocation()}'。
    • @ThinkinBits '{$this->equipment->getInformation()}' 像这样。
    • 我在所有 varchar/text 字段中添加了引号,并保持原样;仍然没有运气
    • echo $query 的结果是什么?另外,查询“描述设备”并粘贴回显该输出的结果。
    【解决方案3】:

    Varchar、char 和文本字段需要在它们周围加上引号才能插入。回显 SQL 查询并检查是否发生了这种情况。

    【讨论】:

      【解决方案4】:

      据我目前所见,您的问题是没有引号的年份字段...

      INSERT INTO 设备(cat_id、名称、 年份、制造商、型号、价格、 位置,条件,stock_num, 信息、描述、创建、 修改)值(1,'r',1,'sdf', 'sdf', '2', 'd', 'd', '3', 'asdfasdfdf', 'df', '10 年 5 月 10 日', '10 5 月 10 日')

      这里的问题是带引号的 cat_id

      查询:INSERT INTO 设备 (cat_id, 名称、年份、制造商、型号、 价格、位置、状况、库存编号、 信息、描述、创建、 修改)值('1','asdf','234', 'adf','asdf','34','asdf','asdf', '234','asdf','asdf','10 年 5 月 10 日', '2010 年 5 月 10 日')

      【讨论】:

      • INSERT INTO 设备(cat_id、名称、prod_year、制造商、型号、价格、位置、条件、stock_num、信息、描述、创建、修改)VALUES (1, 'asdf', '234', 'adf', 'asdf', '34', 'asdf', 'asdf', '234', 'asdf', 'asdf', '10 May 10', '10 May 10') 仍然产生语法错误
      • @ThinkingInBits:您的定义中没有 prod_year。
      • - 您是否也将餐桌上的年份字段更改为 prod_year?那个字段还是varchar吗? --- 表名拼写正确吗? --- 您是否在命令行中尝试过该查询? --- 摆脱想法! :-)
      • 我将年份和名称更改为 prod_year 和 prod_name 只是为了查看是否存在某种命名冲突。我将这个 SQL 直接输入到 phpMyAdmin 中,但出现语法错误。
      • 您可以从该表中选择任何数据吗?您是否有可能在错误的数据库中执行语句?
      【解决方案5】:

      BLEHBLEHSDFOHSE -- 很明显,“条件”也是一个保留字……加上一些反引号,然后它就开始工作了。

      【讨论】:

      • 如果它有效,请返回并将这些答案之一标记为“the”答案,以便我们继续并停止尝试回答问题...
      【解决方案6】:

      停止使用 mysql_query() 并切换到 mysqli 和 prepared statements,那么您就不需要 SQL 字符串中的所有钝的 OOP。它看起来像这样更干净:

      $query = 'INSERT INTO table (id,name,age) VALUES (?,?,?)';
      

      【讨论】:

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