开发者

MYSQL - empty string == 0 problem (updates row with primary key 0 instead of insert)

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, 
                                modu开发者_运维知识库le_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!


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)) 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜