开发者

MySQL: UPDATE Vs INSERT for a INT column

Im currently using CodeIgniters active record class to UPDATE a MySQL table. One of the columns is an INT who's default value I've set to NULL and set the NULL field to TRUE. When I INSERT a record and leave that particular field blank in the form, no problem, it's set to NULL. However, when I do an UPDATE with the field blank in the form, MySQL sets the value to 0. Is there a way to have MySQL intepret an UPDATE the same way an INSERT is done i.e. if the form value 开发者_运维技巧is blank it sets it to NULL and not 0?

Cheers


Most likely empty string '' is sent to the database thus it is cast to INT field as 0 - check exact SQL query (eg. by enabling profiler: $this->output->enable_profiler(TRUE) in your controller). If that's the case add a snippet to your model that will replace '' with NULL while updating.


Have you run a trace on the database to see exactly what SQL CodeIgniters is sending the database? It sounds like CodeIgniters may be sending the zero to MySQL...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜