Php 我无法将codeigniter中的日期插入mysql日期列
控制器Php 我无法将codeigniter中的日期插入mysql日期列,php,mysql,codeigniter,codeigniter-2,Php,Mysql,Codeigniter,Codeigniter 2,控制器 $date=date("Y-d-m"); $succes=$this->pl->insert_ad($brandID,$model,$price,$year,$picture,$resized,$fuels,$id_usera,$date); $date=date("Y-m-d"); 型号 public function insert_ad($brand,$model,$price,$year,$picture,$resized,$fuels,$date) { $sql
$date=date("Y-d-m");
$succes=$this->pl->insert_ad($brandID,$model,$price,$year,$picture,$resized,$fuels,$id_usera,$date);
$date=date("Y-m-d");
型号
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
,我现在真的不知道为什么它不会像其他东西一样插入
表结构
请检查数据库表中“日期”列的数据类型。可能是个问题 尝试以下功能: 日期(“是/月/日”)
日期(“h-m-s”)
日期(“Y/m/d:h-m-s”)
我特别使用这些来获取日期和时间。也可能对您有所帮助。从
$date=date("Y-d-m");
到
和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();
}
更改
插入广告功能,如下所示:
$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();
同时删除控制器函数调用和模型函数定义中的日期参数。controller
$date=date("Y-d-m");
$succes=$this->pl->insert_ad($brandID,$model,$price,$year,$picture,$resized,$fuels,$id_usera,$date);
$date=date("Y-m-d");
数据库日期格式是Y-m-d
而不是Y-d-m
这样进行查询。使用DATE(“.$DATE”)
请试试这个:
控制器:
$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();
}
型号:
$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();
}
**答案**
*日期的正确格式*$date=date(“Y-m-d”)代码>*和*$date=date(“Y-d-m”)代码>以及
尝试在数据库结构中使用varchar类型来表示日期。这项工作非常适合我
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.
在控制器功能中
$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);
试试这个
在控制器中删除变量$data
并且在模型中删除变量
模型
在你的数据库中也有变化
类型为日期时间和
预先确定的作为当前时间
然后运行它
希望这能奏效 还共享表模式?使用$date=date(“Y-m-d”)代码>echo$this->db->last_query()代码>并查看正在构建什么查询最后,使用此代码添加它将插入此1970-01-01@VBProgramming您可以共享表架构吗?@VBProgramming以及echo$sql1的值?如果$sql1返回更新的ans,请立即尝试?我问的是您的完整sql查询?不是真/假完整查询字符串,先生?echo$sql1=“插入<代码>广告
(型号
,年份
,价格
,照片
,日期
,调整大小的照片
,标识燃料
,标识品牌
)值(“$model.”,“$year.”,“$price.”,“,“$picture.”,“$date,”,“$picture.”,“$date,”,“$resized.”,“$brand.”;仅就日期而言:日期(“Y/m/d”);时间:日期(“h-m-s”);对于日期和时间日期(“Y/m/d:h-m-s”);实际上没有,对不起,您可以尝试将“date”列更改为其他任何内容,因为“date”是系统关键字,不建议将其用作列名。