简体   繁体   English

codeigniter插入语法错误

[英]codeigniter insert into syntax error

this is my insert sql statement: 这是我的insert sql语句:

$sql = "
                LOCK TABLE notre_offre WRITE;

                SELECT 
                    @myRight := rgt FROM notre_offre
                WHERE id = " . $this->input->post('category') . ";

                UPDATE notre_offre SET rgt = rgt + 2 WHERE rgt > @myRight;
                UPDATE notre_offre SET lft = lft + 2 WHERE lft > @myRight;
                INSERT INTO notre_offre(id, naziv, lft, rgt) VALUES(null, '" . $this->input->post('title') . "', @myRight + 1, @myRight + 2);
                UNLOCK TABLES;
                ";

        $query = $this->db->query($sql);

But I got syntax error: 但我得到语法错误:

"Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT @myRight := rgt FROM notre_offre WHERE id = 2; UPD' at line 3

What is here problem? 这是什么问题? This works perfectly from phpmyadmin and cmd. 这完全适用于phpmyadmin和cmd。 "

Don't know Codeigniter specifically, but you probably can not send more than one SQL command in one go. 不知道具体的Codeigniter,但你可能不能一次发送多个SQL命令。 Try to send the individual LOCK , SELECT etc. commands with separate query() calls. 尝试使用单独的query()调用发送单独的LOCKSELECT等命令。

You might have to tweak this, but something along these lines should get you started with creating a stored procedure in your database instead of trying to query it every time on the fly: 您可能需要对此进行调整,但是这些内容应该让您开始在数据库中创建存储过程,而不是每次都在运行时尝试查询它:

DROP PROCEDURE IF EXISTS `InsertNode`;
DELIMITER $$
CREATE DEFINER=`db_user`@`localhost` PROCEDURE `InsertNode` (
    pParentCategory VARCHAR(50),
    pCategory VARCHAR(50),
    pTitle VARCHAR(50)
)
    COMMENT 'Inserts a node into a hierarchical table'
    LANGUAGE SQL
    NOT DETERMINISTIC
    MODIFIES SQL DATA
    SQL SECURITY DEFINER
BEGIN
    DECLARE myRight INTEGER;

    START TRANSACTION;
    SELECT `rgt` INTO myRight
      FROM `notre_offre`
      WHERE `id` = pParentCategory;

    UPDATE `notre_offre` SET `rgt` = `rgt` + 2 WHERE `rgt` > myRight;
    UPDATE `notre_offre` SET `lft` = `lft` + 2 WHERE `lft` > myRight;
    INSERT INTO `notre_offre` (`id`, `naziv`, `lft`, `rgt`)
        VALUES (pCategory, pTitle, myRight + 1, myRight + 2);

    SELECT `pCategory` AS "id", pTitle as "myRight",
        (myRight + 1) AS "lft", (myRight + 2) AS "rgt";
    COMMIT;
END $$
DELIMITER ;

Note that you only have to define this once , not every time you want to insert a node. 请注意,您只需要定义一次 ,而不是每次都要插入节点。 As such, you can run it from your favorite DB GUI tool such as PhpMyAdmit, MySQL Workbench, etc. After that, to insert a node, instead of trying to insert it directly into the table, you would call it like this: 因此,您可以从您喜欢的数据库GUI工具(如PhpMyAdmit,MySQL Workbench等)运行它。之后,要插入节点,而不是尝试将其直接插入表中,您可以这样调用它:

CALL `InsertNode`('Televisions', 'Game Consoles', 'User-defined Title');

Like I said, though, depending on the exact fields in your table, you might have to add parameters and tweak the procedure above to work exactly how you want it to. 就像我说的那样,根据表中的确切字段,您可能需要添加参数并调整上面的过程以完全按照您的需要工作。 Still, it should be a good start. 不过,这应该是一个好的开始。

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

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