php
  • mysql
  • sql-insert
  • mysql-error-1064
  • 2016-12-30 76 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',當然,這是有效的語法。 –

    相關問題