Welcome Guest, Not a member yet? Register   Sign In
How can I use active record to insert DEFAULT VALUE?
#1

[eluser]fell0206[/eluser]
Hello everybody, I have a problem with the active record of insert.
My table have a "Root" of column(not null), it have a default value(0), I create a class, and I use "$this->db->insert('mytable',$data);" to insert the record, but when the variable "Root" is null, then the error will occur.
How can I fix this error!! Thanks~

MyTable:
Code:
CREATE TABLE IF NOT EXISTS `test` (
  `A` int(1) NOT NULL,
  `Root` int(1) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

MyClass:
Code:
class MyClass{
   public $A;
   public $Root;
}

Controller:
Code:
...
$myClass=new MyClass();

$this->load->model('test_model');
$myClass->$A=$this->input->post('A');
$this->test_model->add($myClass);
...

Model:
Code:
...
function add($myClass){
   $this->db->insert('test',$myClass);
}
...
#2

[eluser]theprodigy[/eluser]
Not sure if what you show in your code is copy/pasted, but shouldn't
Code:
...
$myClass->$A=$this->input->post('A');
...

be

Code:
...
$myClass->A=$this->input->post('A'); //Remove the $ from $A in $myClass->$A
...

Also, what is the output of this?

Code:
function add($myClass){
   //$this->db->insert('test',$myClass);
   echo $this->db->insert_string('test', $myClass);
}
#3

[eluser]fell0206[/eluser]
Sorry, it's my fault.
In my file it is correct:
Code:
$myClass->A=$this->input->post('A');

About insert string, if variable not set, it will be NULL.(if column have CURRENT_TIMESTAMP and AUTO_INCREAMENT, it work corrent).
How can I fix it, thank you~
#4

[eluser]theprodigy[/eluser]
Quote:About insert string, if variable not set, it will be NULL.
I'm looking for the ACTUAL output of the string, not 'what it will be'. Please run the code, and copy/paste the output of it.




Theme © iAndrew 2016 - Forum software by © MyBB