Bumblebee989
Bumblebee989

Reputation: 19

codeigniter insert into syntax error

this is my insert sql statement:

$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. "

Upvotes: 1

Views: 366

Answers (2)

King Skippus
King Skippus

Reputation: 3826

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:

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.

Upvotes: 1

Wolfgang Stengel
Wolfgang Stengel

Reputation: 2856

Don't know Codeigniter specifically, but you probably can not send more than one SQL command in one go. Try to send the individual LOCK, SELECT etc. commands with separate query() calls.

Upvotes: 2

Related Questions