繁体   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;

    }
  • 顶部的所有回显都显示适合数据库模式的有效数据。
  • 没有连接错误

有任何想法吗?

提前致谢!

这是回声查询

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

MySQL正在给出:#1064-您的SQL语法有错误; 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'condition,stock_num,信息,说明,创建,修改的附近使用)VALUES(1,第1行的'r'

id int(11)无符号NO PRI NULL auto_increment编辑删除cat_id int(11)无符号NO NULL
编辑删除prod_name varchar(255)是NULL
编辑删除prod_year varchar(10)是NULL
编辑删除制造商varchar(255)是空
编辑删除模型varchar(255)是NULL
编辑删除价格varchar(10)是空
编辑删除位置varchar(255)是空
编辑删除条件varchar(25)是NULL
编辑删除stock_num varchar(128)是NULL
编辑删除信息文本是NULL
编辑删除描述文字是NULL
编辑删除创建的varchar(20)是NULL
编辑删除已修改的varchar(20)是NULL

查询: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')

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

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

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

假设位置,信息和描述为字符串引号缺失。

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和text字段需要用引号引起来。 回显SQL查询并检查是否正在发生。

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

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

在这里,问题是带引号的cat_id

查询: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-显然,“条件”也是一个保留字...加上一些反引号,然后开始起作用。

停止使用mysql_query()并切换到mysqli和prepared语句 ,然后您就不需要在SQL字符串中使用所有令人讨厌的OOP了。 这样看起来会更干净:

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

暂无
暂无

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

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