SoFunction
Updated on 2025-04-04

Thinkphp summary of solving the problem of special characters passed into the database

thinkphp solves the problem of data being passed into special characters in database

To solve this problem, you need to make sure that the text content is not encoded in HTML entities before inserting it into the database. If you are getting data from the front end, check whether to use it before sending the requesthtmlspecialchars()Or similar methods have been escaped. If it is an escape caused by backend processing logic, unescaping or avoiding escaping the database before inserting it.

If it is under the Laravel framework, it is generally not necessary to manually deal with such escape problems, because Eloquent ORM does not automatically encode HTML entities when processing strings. But if you do encounter this situation, you can use PHP'shtml_entity_decode()Function restores the original string

example

$operation_code = html_entity_decode($name, ENT_QUOTES, 'UTF-8');
$data = [
    'operation_seq_num' => $num,
    'wip_entity_name' => $wip_entity_name,
    'operation_code' => $operation_code,
    'creation_date' => time(),
    'begin_date' => time(),
    'employee_num' => $employee_num,
    'created_by' =>  $username
];
db::table('wip_production')->insert($data);

Extensions:

How to deal with large-scale concurrent write problem

1. Large-scale concurrent writes, database locking, data writing failure, data table corruption and other problems

2. The lock table needs to be used in conjunction with transactions

3. The specific code is as follows

M()->startTrans();
$lot_check = M()->table('order')->lock(true)->where(['id' => $id])->field('id,type')->find();
$data = ['status'=>2];
$res = M()->save($data);
if($res)
{
M()->commit(); #Submit transaction}else{
M()->rollback(); #Rolleate transactions}

4. The main method adopted is to cooperate with the lock table to solve the problem of large concurrent writes.

This is the article about thinkphp solving the problem of special characters in data transfer to database. For more related thinkphp special characters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!