VBProgramming
VBProgramming

Reputation: 31

I cant insert date from codeigniter into mysql DATE column

CONTROLLER

$date=date("Y-d-m");
$succes=$this->pl->insert_ad($brandID,$model,$price,$year,$picture,$resized,$fuels,$id_usera,$date);

Model

public function insert_ad($brand,$model,$price,$year,$picture,$resized,$fuels,$date)
{
 $sql1="INSERT INTO `ads`(`model`, `year`, `price`, `photo`, `date`, `resized_photo`, `id_fuel`, `id_brand`)
         VALUES ('".$model."','".$year."','".$price."','".$picture."','".$date."','".$resized."','".$fuels."','".$brand."')";
  $this->db->query($sql1);
  return $this->db->insert_id();
}

I print it with print_r($date) and it displays 2016-03-16, I really dont now why it wont insert like everything else.

Table structure enter image description here

Upvotes: 1

Views: 12602

Answers (11)

Yorsh
Yorsh

Reputation: 560

try this

in your Controller delete your variable $data and also in your model delete the variable

model

    public function insert_ad($brand,$model,$price,$year,$picture,$resized,$fuels)
{
 $sql1="INSERT INTO `ads`(`model`, `year`, `price`, `photo`, `resized_photo`, `id_fuel`, `id_brand`)
         VALUES ('".$model."','".$year."','".$price."','".$picture."','".$date."','".$resized."','".$fuels."','".$brand."')";
  $this->db->query($sql1);
  return $this->db->insert_id();
}

and also in your db change type as DATETIME and PREDETERMINATED as CURRENT_TIME and run it

hope this works!

Upvotes: 0

Parvez Ahmed
Parvez Ahmed

Reputation: 650

this work perfect for me

in controller function

$this->model_invoice->invoice_create();

in model

  $invoice_date = strtotime($this->input->post('invoice_date_time'));
     $date=date("Y-m-d", $invoice_date);

    $data = array
   (
    'invoice_date_time' =>  $date,
    );

    $this->db->insert('your_table_name', $data);

Upvotes: 0

Apoorv
Apoorv

Reputation: 231

CONTROLLER

$date=date("Y-m-d");

Database date format is Y-m-d not Y-d-m

Upvotes: 0

Enriqho Juan
Enriqho Juan

Reputation: 31

I just tried this minutes ago and it worked! ^_^

$d = date("Y-m-d");
$t = date("h:i:s");

$this->db->set($this->col_prefix.'customer_id', $_POST['customer_id']); 
$this->db->set($this->col_prefix.'total_amount', $_POST['total_amount']); 
$this->db->set('date', "'".$d."'"); 
$this->db->set('time_in', "'".$t."'"); 
$this->db->set('status', 1); 
$this->db->insert('sln_transactions');

but haven't tried it yet in array :) hope this could help.

Upvotes: 1

Hanan Ashraf
Hanan Ashraf

Reputation: 518

Try by using type varchar in your database structure for date.

Upvotes: 0

VBProgramming
VBProgramming

Reputation: 31

**ANSWER **
*The correct formats of date * $date=date("Y-m-d"); *and * $date = date("Y-d-m"); as well

 public function insert_ad($brand,$model,$price,$year,$picture,$resized,$fuels,$date)
 {
     $sql1="INSERT INTO `ads` (`id_ad`, `model`, `year`, `price`, `photo`, `date`, `resized_photo`, `id_fuel`, `id_brand`) VALUES (NULL, '$model', '$year', '$price', '$picture', '".date('Y-m-d')."', '$resized', '$fuels', '$brand')";
     $this->db->query($sql1);
     return $this->db->insert_id();
}

Upvotes: 0

Niklesh Raut
Niklesh Raut

Reputation: 34924

Change From

$date=date("Y-d-m");

To

$date=date("Y-m-d");

And remvoe ' from integer type

public function insert_ad($brand,$model,$price,$year,$picture,$resized,$fuels,$date)
{
 $sql1="INSERT INTO `ads` (`id_ad`, `model`, `year`, `price`, `photo`, `date`, `resized_photo`, `id_fuel`, `id_brand`) VALUES (NULL, '$model', '$year', '$price', '$picture', '".date('Y-m-d')."', '$resized', '$fuels', '$brand')";
  $this->db->query($sql1);
  return $this->db->insert_id();
}

Upvotes: 0

BobGao
BobGao

Reputation: 790

Please try this:

Controller:

    $date=date("Y-d-m");
    $succes=$this->pl->insert_ad(array(
        'model'=>$model,
        'id_brand'=>$brandID,
        'price'=>$price,
        'photo'=>$picture,
        'date'=>$date,
        'resized_photo'=>$resized,
        'id_fuel'=>$id_usera,
        'year'=>$year));

Model:

public function insert_ad($data) {
    $this->db->insert('ads', $data);
    return $this->db->insert_id();
}

Upvotes: 0

Sankar V
Sankar V

Reputation: 4128

Change your insert_ad function as below:

$sql1="INSERT INTO `ads`(`model`, `year`, `price`, `photo`,`resized_photo`, `id_fuel`, `id_brand`,`date`) VALUES ('".$model."','".$year."','".$price."','".$picture."','".$resized."','".$fuels."','".$brand."',NOW())";
$this->db->query($sql1);
return $this->db->insert_id();

Also remove the date parameter in controller function call and model function definition.

Upvotes: 0

rmondesilva
rmondesilva

Reputation: 1792

Make your query like this. Use DATE(".$date.")

$sql1="INSERT INTO `ads`(`model`, `year`, `price`, `photo`, `date`, `resized_photo`, `id_fuel`, `id_brand`)
         VALUES ('".$model."','".$year."','".$price."','".$picture."',DATE(".$date."),'".$resized."','".$fuels."','".$brand."')";

Upvotes: 0

Little Oars
Little Oars

Reputation: 64

Please check data-type of 'date' column in database table. Might be possible issue.

Try these functions :

date('Y/m/d');
date('h-m-s');
date('Y/m/d:h-m-s');

I used these in particular to get the date and time. Might help you as well.

Upvotes: 0

Related Questions