【问题标题】:SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value, html_entity_decode functionSQLSTATE[22007]:无效的日期时间格式:1366 字符串值不正确,html_entity_decode 函数
【发布时间】:2021-05-25 00:59:54
【问题描述】:

我一直在尝试下面的 html_entity_decode;

//Foreach ($datas from remote db)
$recorded = User::updateOrCreate([
        "old_id" => $user->Id,
        "username" => $user->UserName,
    ],[
        "old_id" => $user->Id,
        "name" => html_entity_decode($user->FullName),
        "username" => $user->UserName
]

它给了我如下错误,

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xE3\xBCseyi...' for
column `database`.`users`.`name` at row 1 (SQL: update `users` set
`name` = hseyin furkan, `users`.`updated_at` = 2021-02-22 18:23:39 where `id` = 2)

如果我不调用 html_entity_decode 函数,它会写入“hüseyin”之类的列

我该如何解决?

我的桌子:

Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name')->nullable();
        $table->string('username')->nullable();

【问题讨论】:

  • 请分享您的输入字符串
  • 但可能你的name列类型是datetime,你需要改成varchar
  • 不,我的名字列字符串

标签: php html laravel eloquent laravel-8


【解决方案1】:

可能,您的数据是 ASCII 字符集,而您的列等待 UTF-8 你能试试这个代码吗?

mb_convert_encoding(html_entity_decode($user->FullName), "UTF-8")

如果你使用laravel,你可以使用那个函数;

$fullname = Str::ascii(html_entity_decode($user->FullName),'tr');

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 2021-02-14
    • 2021-07-23
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2017-12-10
    • 2018-06-24
    相关资源
    最近更新 更多