我无法从codeigniter插入日期到mysql DATE列

时间:2016-03-16 07:42:13

标签: php mysql codeigniter codeigniter-2

CONTROLLER

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

模型

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();
}

我用print_r($date)打印它并显示2016-03-16,我现在真的不知道为什么它不会像其他所有内容一样插入。

表结构 enter image description here

11 个答案:

答案 0 :(得分:1)

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.

答案 1 :(得分:0)

请检查数据库表中“日期”列的数据类型。可能会有问题。

尝试以下功能:

  

日期( 'Y / M / d');
  日期( 'H-M-S');
  日期( 'Y / M / d:H-M-S');

我特别使用这些来获取日期和时间。也可以帮助你。

答案 2 :(得分:0)

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

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

来自整数类型的remvoe '

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();
}

答案 3 :(得分:0)

更改insert_ad功能,如下所示:

$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();

同时删除控制器函数调用和模型函数定义中的日期参数。

答案 4 :(得分:0)

<强> CONTROLLER

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

数据库日期格式为Y-m-d而非Y-d-m

答案 5 :(得分:0)

像这样进行查询。使用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."')";

答案 6 :(得分:0)

请试试这个:

<强>控制器:

    $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));

<强>型号:

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

答案 7 :(得分:0)

**答案**
*正确的日期格式* $date=date("Y-m-d"); *和* $date = date("Y-d-m"); 以及

 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();
}

答案 8 :(得分:0)

尝试在数据库结构中使用type varchar作为日期。

答案 9 :(得分:0)

这项工作对我来说很完美

在控制器功能中

$this->model_invoice->invoice_create();

在模型中

  $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);

答案 10 :(得分:0)

尝试

在您的控制器中删除变量$ data  并在您的模型中删除变量

模型

    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();
}

以及您的数据库更改 键入 DATETIME ,然后 PREDETERMINATED 作为 CURRENT_TIME 并运行它

希望这行得通!