php
  • mysql
  • sql-insert
  • mysql-error-1064
  • 2016-12-30 54 views 2 likes 
    2

    下面是我的表,它是类似于另一个现有的表。但是执行查询时出现一个错误。MySQL错误号:1064当插入到数据库

    // 
    public function addFirstChild() { 
        $this->db->query("INSERT INTO " . $this->db->table("genealogy") . " 
        WHERE parent_id  = '" . (int)$this->getSponsorID() . "' 
            SET first_child  = '" . (int)$this->getId()."', 
            genealogy_id  = '" . (int)$this->getId() ."'"); 
    
    } 
    

    执行时,我得到了以下错误:

    SQL Error: 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 'WHERE parent_id = '1' SET first_child = '2', ' at line 2 Error No: 1064 SQL: INSERT INTO ci_genealogy WHERE parent_id = '1' SET first_child = '2', genealogy_id = '2' in C:\wamp64\www\s1nb2\core\database\amysqli.php on line 108

    还有,我能想到的来拉这个功能没有其他办法,我在网上搜索,并用同样的错误读取多个职位,但仍然无解。请帮忙。我花了4个多小时试图做到这一点。

    +0

    @JayBlanchard权在指甲上出现。 –

    +1

    如果答案解决了您的问题,请考虑接受答案。以下是http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work然后返回到此处,并使用勾号/复选标记执行相同操作,直至变为绿色。这告知社区,找到了解决方案。否则,其他人可能会认为这个问题仍然存在,并且可能需要发布(更多)答案。您将获得积分,其他人将被鼓励帮助您。 *欢迎使用Stack!* –

    +0

    或者如果您需要插入,则可以在'values(...)'子句或'set'子句中提供所有值,不要使用'where'。 – Shadow

    回答

    3

    由于您使用的是WHERE,因此您希望更改现有记录。你想要一个UPDATE,不是INSERT

    "UPDATE " . $this->db->table("genealogy") . " 
    SET first_child = '" . (int)$this->getId()."', genealogy_id = '" . (int)$this->getId() ."' 
    WHERE parent_id = '" . (int)$this->getSponsorID() . "'" 
    
    +0

    @Shadow我认为Jay可能意味着要使用'WHERE',他们需要使用'INSERT ... SELECT' http://dev.mysql.com/doc/refman/5.7/en /insert-select.html,以便在使用'INSERT'时使用'WHERE'子句--OP的语法不正确。显然,'UPDATE'是这里的解决方案。 –

    +0

    谢谢Jay!更新做到了。 –

    +0

    @Shadow我的(编辑)评论附录,'INSERT'可以使用'SET',当然,这是有效的语法。 –

    相关问题