热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

运行Insert语句时,碳日期为空-CarbondateisemptywhenIrunInsertstatement

ImstarintouseCarbontomanageDatesinPHPwithMySQL,butImhavingproblemswithasimpleIn

I'm starin to use Carbon to manage Dates in PHP with MySQL, but I'm having problems with a simple Insert statement. To run SQL statements I'm using Medoo, this is my code to insert:

我很想使用Carbon来管理带有MySQL的PHP​​中的Dates,但是我遇到了一个简单的Insert语句问题。要运行我正在使用Medoo的SQL语句,这是我要插入的代码:

public function register($code, $fullname, $address, $phones, $email, $now){

    $registration = $this->_app['medoo']->insert("customers",[
        'cus_code' => $code,
        'cus_fullname' => $fullname,
        'cus_address' => $address,
        'cus_phone_s' => $phones,
        'cus_email' => $email,
        'created_at' => $now
    ]);


    return $registration;

}

And this is where I pass the value:

这是我传递价值的地方:

$code = $request->get('code');
$name = $request->get('name');
$phOnes= $request->get('phone_s');
$address = $request->get('address');
$email = $request->get('email');
$now = Carbon::now('America/Monterrey');

$customer_registration = $app['customer.model']->register($code, $name, $address, $phones, $email, $now);`

if($customer_registration){
     $request_status = true;
 }else {
     $request_errors[] = "Ocurrio un error al registrar el cliente";
 }

The statement returns an error and this is the output if I debug it:

该语句返回一个错误,如果我调试它,这是输出:

INSERT INTO "customers" 
("cus_code", "cus_fullname", "cus_address", "cus_phone_s", "cus_email", "created_at") 
 VALUES 
('n20', 'notaria 20', 'lerdo 202', '1234567890', 'mail@mail.com')

As you can see, the date is missing and It doesn't make any sense because if I echo it inside my function...

正如你所看到的,日期缺失了,它没有任何意义,因为如果我在我的函数中回应它...

public function register($code, $fullname, $address, $phones, $email, $now){
   echo '

this is the variable now: '.$now.'

'; $this->_app['medoo']->debug()->insert("customers",[ 'cus_code' => $code, 'cus_fullname' => $fullname, 'cus_address' => $address, 'cus_phone_s' => $phones, 'cus_email' => $email, 'created_at' => $now ]); die(); return $registration; }

outputs...

  this is the variable now: 2017-05-14 00:52:00

  INSERT INTO "customers" 
  ("cus_code", "cus_fullname", "cus_address", "cus_phone_s", "cus_email", "created_at") 
  VALUES 
  ('n20', 'notaria20', 'lerdo 202', '1234567890', 'mail@mail.com')

I changed $now for Date('Y-m-d H:m:s') and It works, but for my app I need to use Carbon.

我现在为Date更改$('Y-m-d H:m:s')并且它有效,但对于我的应用程序,我需要使用Carbon。

I hope I was clear with this, I'm not so good with english, please help!

我希望我对此很清楚,我对英语不太满意,请帮忙!

1 个解决方案

#1


0  

The answer of this question is in comments and which is not the way to post in SO so I am posting the answer here to close the question.

这个问题的答案是在评论中,而不是在SO中发布的方式,所以我在这里发布答案来结束这个问题。

Here he needs to pass timestamp in SQL format as laravel's insert() doesn't affect created_at and updated_at values and needs manual efforts to add created_at values, so the correct way to pass it into insert() method manually should be:

在这里,他需要以SQL格式传递时间戳,因为laravel的insert()不会影响created_at和updated_at值,需要手动添加created_at值,因此将其手动传递给insert()方法的正确方法应该是:

Carbon::now('America/Monterrey')->format('Y-m-d H:m:s');

Thus the code should look like this

因此代码看起来应该是这样的

$now =Carbon::now('America/Monterrey')->format('Y-m-d H:m:s');
$this->_app['medoo']->debug()->insert("customers",[
        'cus_code' => $code,
        'cus_fullname' => $fullname,
        'cus_address' => $address,
        'cus_phone_s' => $phones,
        'cus_email' => $email,
        'created_at' => $now
    ]);

推荐阅读
author-avatar
陈庭勇筱玲喜芳
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有