Welcome Guest, Not a member yet? Register   Sign In
CURDATE() and INTERVAL in codeigniter
#21

[eluser]Mauricio de Abreu Antunes[/eluser]
[quote author="Thiago Leao" date="1329320162"]when I insert manually in mysql, it works perfectly!!!!
It is giving some problem in converting to CI!

Code:
INSERT INTO buy (id_products, date_start, date_finish) VALUES ('1', CURDATE(), CURDATE() + INTERVAL 30 DAY)
[/quote]

Ok,
Print your query for us:
Code:
$this->db->insert_string();

Use your array listed above this post and try out this method (dont use insert, print the insert_string result.

Smile
#22

[eluser]Thiago Leao[/eluser]
function insertBuy($id_products){
$this->load->helper('date');
$array = array('id_products' => $id_products, 'id_user' => '0102100', 'date_start' => 'CURDATE()', 'date_finish' => 'DATE_ADD(CURDATE() + INTERVAL 30 DAY)', 'valor_venda' => '50');
$this->db->insert_string('buy', $array);

}

nothing...

thanks!
#23

[eluser]Mauricio de Abreu Antunes[/eluser]
Code:
function insertBuy($id_products) {
  $this->load->helper('date'); //Is it necessary?
  $arr_products = array('id_products' => $id_products,
                 'date_start' => 'CURDATE()',
                 'date_finish'=> 'CURDATE() + INTERVAL 30 DAYS');
  echo $this->db->insert_string('buy', $arr_products);
}
#24

[eluser]Thiago Leao[/eluser]
INSERT INTO `buy` (`id_products`, `date_start`, `date_finish`) VALUES ('341', 'CURDATE()', 'CURDATE() + INTERVAL 30 DAYS')

The date functions are going to string!


------------------------------------------------------

What I see is that I will have to use the helper date because the date function of php will not work!

Code:
function insertBuy($id_products, $id_user){
  $this->load->helper('date');
  $datestring = "%Y-%m-%d";
  $time = time();
  $array = array('id_products' => $id_products, 'id_user' => $id_user, 'date_start' => mdate($datestring, $time), 'date_finish' => 'mdate($datestring, $time)+ INTERVAL 30 DAY', 'valor_venda' => '50');
  $teste = $this->db->insert_string('buy', $array);
  echo $teste;
  
}

PRINT = INSERT INTO `buy` (`id_products`, `id_user`, `date_start`, `date_finish`, `valor_venda`) VALUES ('340', '26fe30c72414ab935f3cf8eb531b6b5e', '2012-02-15', 'mdate($datestring, $time)+ INTERVAL 30 DAY', '50')

Now we need to add 30 days +!!!
#25

[eluser]Mauricio de Abreu Antunes[/eluser]
[quote author="Thiago Leao" date="1329322797"]INSERT INTO `buy` (`id_products`, `date_start`, `date_finish`) VALUES ('341', 'CURDATE()', 'CURDATE() + INTERVAL 30 DAYS')

The date functions are going to string!


------------------------------------------------------

What I see is that I will have to use the helper date because the date function of php will not work![/quote]

CURDATE is a MySQL function.
CURDATE returns YYYY-MM-DD.

Your values in your insert string are right!

If you have a DATETIME column in your table and you insert
Code:
'insert into dates_purchase (date_pur) values NOW()'

Btw, run your query in your database using your MySQL Tool.
#26

[eluser]Thiago Leao[/eluser]
It's always the same problem, it sends as a string date_finish!
#27

[eluser]Mauricio de Abreu Antunes[/eluser]
Dates are passed as string to MySQL. (Read about MySQL datatypes) this way: 'CURDATE()' works in yout insert string.
Please, read my posts again.
I'm done.
Bye!
#28

[eluser]Thiago Leao[/eluser]
Know I know the functions, including data.
No need to insert the date as a string, can be inserted as a function in php.

I'm having difficulty passing the query to active record!
#29

[eluser]Mauricio de Abreu Antunes[/eluser]
[quote author="Thiago Leao" date="1329326181"]Know I know the functions, including data.
No need to insert the date as a string, can be inserted as a function in php.

I'm having difficulty passing the query to active record![/quote]

Ok, let's go again.
When you use any php date function, it returns a string. Smile
This way, that string sql works.
#30

[eluser]Thiago Leao[/eluser]
Does anyone know of a direct way for me to enter my query, without the active record?
INSERT INTO buy (id_products, date_start, date_finish) VALUES ('1', CURDATE(), CURDATE() + INTERVAL 30 DAY)





Theme © iAndrew 2016 - Forum software by © MyBB