简体   繁体   English

这个MySQL查询有问题吗?

[英]Is there a problem with this MySQL Query?

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;

    }
  • The echos at the top all display valid data that fit the database schema. 顶部的所有回显都显示适合数据库模式的有效数据。
  • There are no connection errors 没有连接错误

Any ideas? 有任何想法吗?

Thanks in advance! 提前致谢!

Here is the echo'ed query 这是回声查询

INSERT INTO equipment (cat_id, name, year, manufacturer, model, price, location, condition, stock_num, information, description, created, modified) VALUES (1, 'r', 1, 'sdf', 'sdf', '2', 'd', 'd', '3', 'asdfasdfdf', 'df', '10 May 10', '10 May 10') 插入设备(cat_id,名称,年份,制造商,型号,价格,位置,条件,stock_num,信息,描述,创建,修改)值(1 、、'r',1,'sdf','sdf','2 ','d','d','3','asdfasdfdf','df','10 May 10','10 May 10'))

MySQL is giving: #1064 - You have an error in your SQL syntax; MySQL正在给出:#1064-您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition, stock_num, information, description, created, modified) VALUES (1, 'r' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'condition,stock_num,信息,说明,创建,修改的附近使用)VALUES(1,第1行的'r'

id int(11) unsigned NO PRI NULL auto_increment Edit Delete cat_id int(11) unsigned NO NULL id int(11)无符号NO PRI NULL auto_increment编辑删除cat_id int(11)无符号NO NULL
Edit Delete prod_name varchar(255) YES NULL 编辑删除prod_name varchar(255)是NULL
Edit Delete prod_year varchar(10) YES NULL 编辑删除prod_year varchar(10)是NULL
Edit Delete manufacturer varchar(255) YES NULL 编辑删除制造商varchar(255)是空
Edit Delete model varchar(255) YES NULL 编辑删除模型varchar(255)是NULL
Edit Delete price varchar(10) YES NULL 编辑删除价格varchar(10)是空
Edit Delete location varchar(255) YES NULL 编辑删除位置varchar(255)是空
Edit Delete condition varchar(25) YES NULL 编辑删除条件varchar(25)是NULL
Edit Delete stock_num varchar(128) YES NULL 编辑删除stock_num varchar(128)是NULL
Edit Delete information text YES NULL 编辑删除信息文本是NULL
Edit Delete description text YES NULL 编辑删除描述文字是NULL
Edit Delete created varchar(20) YES NULL 编辑删除创建的varchar(20)是NULL
Edit Delete modified varchar(20) YES NULL 编辑删除已修改的varchar(20)是NULL

Query: INSERT INTO equipment (cat_id, prod_name, prod_year, manufacturer, model, price, location, condition, stock_num, information, description, created, modified) VALUES (1, 'asdf', '234', 'adf', 'asdf', '34', 'asdf', 'asdf', '234', 'asdf', 'asdf', '10 May 10', '10 May 10') 查询:INSERT INTO设备(cat_id,prod_name,prod_year,制造商,型号,价格,位置,条件,stock_num,信息,描述,创建,修改)值(1 、、'asdf','234','adf','asdf ','34','asdf','asdf','234','asdf','asdf','10 May 10','10 May 10')

Here is the SQL export from PhpMyAdmin in case someone wants to try duplicating this issue: http://pastie.org/954206 如果有人想尝试复制此问题,这是从PhpMyAdmin导出的SQL: http : //pastie.org/954206

BLEHBLEHSDFOHSE -- So apparently, 'condition' is also a reserved word... threw in some backticks, then it started working. BLEHBLEHSDFOHSE-显然,“条件”也是一个保留字...加上一些反引号,然后开始起作用。

YEAR is a reserved word in MySQL. YEAR是MySQL中的保留字。 If you're going to use it, you're going to need to backtick (ie `year` ) 如果要使用它,则需要反引号(即`year`

Assuming location, information and description are strings quotes are missing. 假设位置,信息和描述为字符串引号缺失。

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')";

Varchar, char and text fields need quotes around them to insert. Varchar,char和text字段需要用引号引起来。 Echo out the SQL query and check this is happening. 回显SQL查询并检查是否正在发生。

as far as i see it at this point, your problem was year field with no quotes... 据我目前所见,您的问题是年份字段没有报价...

INSERT INTO equipment (cat_id, name, year, manufacturer, model, price, location, condition, stock_num, information, description, created, modified) VALUES (1, 'r', 1 , 'sdf', 'sdf', '2', 'd', 'd', '3', 'asdfasdfdf', 'df', '10 May 10', '10 May 10') 插入设备(cat_id,名称,年份,制造商,型号,价格,位置,条件,stock_num,信息,描述,创建,修改)值(1,'r', 1 ,'sdf','sdf','2 ','d','d','3','asdfasdfdf','df','10 May 10','10 May 10'))

here, the problem is cat_id WITH quotes 在这里,问题是带引号的cat_id

Query: INSERT INTO equipment (cat_id, name, year, manufacturer, model, price, location, condition, stock_num, information, description, created, modified) VALUES ( '1' , 'asdf', '234', 'adf', 'asdf', '34', 'asdf', 'asdf', '234', 'asdf', 'asdf', '10 May 10', '10 May 10') 查询:INSERT INTO设备(cat_id,名称,年份,制造商,型号,价格,位置,状况,stock_num,信息,描述,创建,修改)VALUES( “ 1” ,“ asdf”,“ 234”,“ adf”, 'asdf','34','asdf','asdf','234','asdf','asdf','10 May 10','10 May 10'))

BLEHBLEHSDFOHSE-显然,“条件”也是一个保留字...加上一些反引号,然后开始起作用。

Stop using mysql_query() and switch to mysqli and prepared statements , then you don't need all that obtuse OOP in your SQL string. 停止使用mysql_query()并切换到mysqli和prepared语句 ,然后您就不需要在SQL字符串中使用所有令人讨厌的OOP了。 It would look cleaner like this: 这样看起来会更干净:

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

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

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