MySQL 插入数据库时​​出现错误编号:1064

MySQL Error No: 1064 when INSERTING to Database

下面是我的 table,它与另一个现有的 table 相似。但是我在执行查询时遇到一个错误。

//
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 个多小时来解决这个问题。

由于您使用的是 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() . "'"