Force mysql to record NULL if field was left empty? -
here situation:
i have optional input fields, don't care if user fills in or not. if leave empty, want null recorded.
and there field example: decimal (10,2), marked can null , default value set null.
and example have input field corresponds it:
<input type="text" name="foo" size="4"/>
if submit form without touching field mysql records 0.00. there way force record null instead?
ps. same goes other field types. example have int well. in case 0
gets recorded.
pps. if go insert given record through phpmyadmin, , leave given fields empty, null values gets recorded desired.
html
<label>svoris: <input type="text" name="load_weight1" size="4"></label> <label>tūris(m<sup>3</sup>)<input type="text" name="load_volume1" size="4"></label>
php
$data["load_weight"] = $this->input->post("load_weight1"); $data["load_volume"] = $this->input->post("load_volume1"); $quoteid = $this->uzklausos_forma_model->saverecord($data);
saverecord()
function saverecord($items) { $this->db->insert('request_quote', $items); return $this->db->insert_id(); }
you handle trigger:
create trigger ins_tbl_null before insert on tbl each row begin if new.value = "" set new.value = null; end if; end;
the above code tell mysql replace before each insert
statement content of column value null if empty.
see http://sqlfiddle.com/#!2/2b259/1 live example
Comments
Post a Comment