代码点火器插入了两次相同的条目.在某些情况下


CodeIgniter is inserting twice the same entry. In some cases

我试图弄清楚为什么Codeigniter在我的数据库中插入了2次相同的行。我正在使用PDO作为mySQL的接口。我正在调试它,我确定下面的函数没有被执行两次。它发生在特定情况下,如果两个前痛因为数组为空而没有运行,但如果其中一个运行,则不会发生错误。

'

public function save_all() //save all information with the launched flag FALSE
{
    include(database_vars_url());
    try
    {
        $this->add_new_skills();
        if(!isset($_SESSION))
        {
            session_start();
        }
        $email = $_SESSION["email"];
        $sql = "INSERT INTO $tbl_name_contest (user_id, contest_title, contest_overview, contest_description,
                contest_category, contest_holder, contest_prize, contest_stage, contest_duration, contest_proj_duration,
                contest_level, contest_finalist, contest_winner, contest_create, contest_launched, contest_edit,
                contest_edit_id, contest_status, contest_delete)
                VALUES (0, '$this->title', '$this->overview', '$this->description', '$this->category',
                (SELECT customer_id FROM $tbl_name_customer WHERE user_id = 
                (SELECT user_id FROM $tbl_name_user WHERE user_email='$email')), '$this->prize', 0, '$this->contest_period',
                '$this->project_period', -1, -1, -1, NULL, DEFAULT, NULL, -1, -1, DEFAULT); ";
        foreach ($this->addon as $value)
        {
            $sql = $sql . "INSERT INTO $tbl_rel_contest_addon (add_contest_id, add_addon_id) 
                            VALUES ((SELECT contest_id FROM $tbl_name_contest WHERE contest_title = '$this->title'
                            AND contest_overview = '$this->overview' AND contest_prize = '$this->prize'),
                            (SELECT addon_id FROM $tbl_name_addon WHERE addon_name = '$value')); ";
        }
        foreach ($this->skills as $value)
        {
            $sql = $sql . "INSERT INTO $tbl_rel_contest_skill (required_contest_id, required_skill_id) 
                            VALUES ((SELECT contest_id FROM $tbl_name_contest WHERE contest_title = '$this->title'
                            AND contest_overview = '$this->overview' AND contest_prize = '$this->prize'),
                            (SELECT skill_id FROM $tbl_name_skill WHERE skill_name = '$value')); ";
        }
        echo $sql;
        return $this->db->query($sql);
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
    return 0;
}

这里我有一个 mySQL 的日志,前两个 SELECT 由函数 save_all() 开头的"$this->add_new_skills();"调用。这些 SELECT 也应该只是一个。此顺序选择选择,插入插入,证明函数 save_all() 没有被调用两次,如果它被调用两次,顺序将是选择插入选择插入。

138 Connect root@localhost on repsero
138 Query SELECT skill_name FROM skill WHERE skill_status=2
138 Query SELECT skill_name FROM skill WHERE skill_status=2
138 Quit
139 Connect root@localhost on repsero
139 Quit
140 Connect root@localhost on repsero
140 Query INSERT INTO contest (user_id, contest_title, contest_overview, contest_description, contest_category, contest_holder, contest_prize, contest_stage, contest_duration, contest_proj_duration, contest_level, contest_finalist, contest_winner, contest_create, contest_launched, contest_edit, contest_edit_id, contest_status, contest_delete) VALUES (0, 'Contest Name', 'Overview of the contest Overview of the contest Overview of the contest Overview of the contest Overview of the contest Overview of the contest ', 'Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description ', 'SEO', (SELECT customer_id FROM customer WHERE user_id = (SELECT user_id FROM user WHERE user_email='customer@repsero.com')), '300', 0, '5','1', -1, -1, -1, NULL, DEFAULT, NULL, -1, -1, DEFAULT) 
140 Query INSERT INTO contest (user_id, contest_title, contest_overview, contest_description, contest_category, contest_holder, contest_prize, contest_stage, contest_duration, contest_proj_duration, contest_level, contest_finalist, contest_winner, contest_create, contest_launched, contest_edit, contest_edit_id, contest_status, contest_delete) VALUES (0, 'Contest Name', 'Overview of the contest Overview of the contest Overview of the contest Overview of the contest Overview of the contest Overview of the contest ', 'Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description ', 'SEO',(SELECT customer_id FROM customer WHERE user_id = (SELECT user_id FROM user WHERE user_email='customer@repsero.com')), '300', 0, '5', '1', -1, -1, -1, NULL, DEFAULT, NULL, -1, -1, DEFAULT)
140 Quit

好吧,我发现编码点火器系统内部CI_DB_pdo_driver中的 _execute() 函数无法正常工作,可能是因为 php 版本。无论如何,我将CodeIgniter的代码从:

    function _execute($sql)
{
    $sql = $this->_prep_query($sql);
    $result_id = $this->conn_id->prepare($sql);
    $result_id->execute();
    if (is_object($result_id))
    {
        if (is_numeric(stripos($sql, 'SELECT')))
        {
            $this->affect_rows = count($result_id->fetchAll());
            $result_id->execute();
        }
        else
        {
            $this->affect_rows = $result_id->rowCount();
        }
    }
    else
    {
        $this->affect_rows = 0;
    }
    return $result_id;
}

自:

    function _execute($sql)
{
    $sql = $this->_prep_query($sql);
    $result_id = $this->conn_id->prepare($sql);
    $result_id->execute();
    if (is_object($result_id))
    {
        if (preg_match('/^'s*"?(SELECT)'s+/i', $sql))
        {
            $this->affect_rows = count($result_id->fetchAll());
            $result_id->execute();
        }
        else
        {
            $this->affect_rows = $result_id->rowCount();
        }
    }
    else
    {
        $this->affect_rows = 0;
    }
    return $result_id;
}

测试$sql内部是否包含"SELECT"的部分不起作用,因此当我尝试插入"$result_id->execute()"时,被调用了两次。

相关文章: