s1nb2
s1nb2

Reputation: 82

MySQL Error No: 1064 when INSERTING to Database

Below is my table which is similar to another existing table. But I get one error when executing the query.

//
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() ."'");

}

When executed I get the below Error:

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

There's no other way that I can think of to pull of this function and I have searched online and read multiple posts with the same error but still no solution. Please help. I spent over 4 hours trying to get this right.

Upvotes: 2

Views: 262

Answers (1)

Jay Blanchard
Jay Blanchard

Reputation: 34416

Since you're using a WHERE it appears that you want to change an existing record. You want an UPDATE, not an INSERT

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

Upvotes: 2

Related Questions