Question

actually this already worked and i have no idea what i've changed so that i run into this problem now. Tried nearly everything.

The case:
I have the following query. The variable $DB-id is an empty string if the content i want to save is new. The field module_id is my primary key. What happens is that this query always updates the row with the module_id => 0.

The problem
Passing an empty variable as the primary key always updates row 0 instead of inserting a new one.

$this->db->query("INSERT INTO   modules_text 
                                (   
                                module_id,
                                module_content,
                                module_page_idx, 
                                module_post_id
                                )
                  VALUES        (
                                '{$DB_id}',
                                '{$content['text']}',
                                '{$content['idx']}',
                                '{$post_id}'
                                )
                  ON DUPLICATE KEY
                  UPDATE        module_content  = '{$content['text']}',
                                module_page_idx = '{$content['idx']}'
                ");

Does anybody have an idea how i can tell MYSQL to create a new row??? Any help is very appreciated!!!! Thank you very much!!!

Saludos Sacha!

Was it helpful?

Solution

You may need to set your primary key field to auto increment. An example is below.

CREATE TABLE tablename (
 id MEDIUMINT NOT NULL AUTO_INCREMENT,
 name CHAR(30) NOT NULL,
 PRIMARY KEY (id)) 

OTHER TIPS

I would assume you need to give the UPDATE a WHERE?
eg:

$this->db->query("INSERT INTO   modules_text 
                                (   
                                module_id,
                                module_content,
                                module_page_idx, 
                                module_post_id
                                )
                  VALUES        (
                                '{$DB_id}',
                                '{$content['text']}',
                                '{$content['idx']}',
                                '{$post_id}'
                                )
                  ON DUPLICATE KEY
                  UPDATE        `module_content`  = '{$content['text']}',
                                `module_page_idx` = '{$content['idx']}'
                  WHERE
                                `module_id` = '{$content['id']}'
                ");

Don't hold me to it xD, just a guess at first glance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top