Php an action is used in more than two places, how to propose it?

two hypotheses

A

$do->query(
        "INSERT INTO `abc` (
          `public_order_id`,
          `add_time`,
          `status`
        )
        VALUES (
          "{$public_order_id}",
          "{$add_time}",
          ""
        ) "
      );

B

$do->query(
        "INSERT INTO `abc` (
          `public_order_id`,
          `add_time`,
          `status`
        )
        VALUES (
          "{$public_order_id}",
          "{$add_time}",
          ""
        ) "
      );

suppose there are more than two places that my action will use
, but the only thing that will change is status
An is "cancelled"
B will change to "completed"
to other places there will be another status
everything else is the same,
then how does a mysql query like this come up?
originally intended to put it in function, but it won"t work.

< hr >

think of a question

if you want to let function use the external value
I can use global to let the external value come in function
but what if he doesn"t look like $abc but like $SESSION ["id"] and $oder [" id"]?

$abc = 1;
function XXX () {
global $abc;
echo $abc; / / can show
}

Apr.04,2022

if all of these are handwritten SQL, then encapsulate them into objects and rely on $do, that is, pass $do when passing parameters

function changeOrderStatus($db,$params){
    //  $db   
    if(!$db instanceof \PDO){
        throw new \Exception('$db not a PDO object instance.');
    }
    $params = array_merge([
        'status'=>'',//status 
        'public_order_id'=>null,
        'add_time'=>date('Y-m-d H:i:s'),
    ],$params);
    if(empty($params['public_order_id'])){
        throw new \Exception('public_order_id is must.');
    }
    return $db->query(
        vprintf('INSERT INTO `abc` (`public_order_id`,`add_time`,`status`) 
            VALUES ("%s","%s","%s");',
                $params['public_order_id']
                ,$params['add_time']
                ,$params['status']
        )
    );
}
// Call
changeOrderStatus($do,[
    'status'=>'',
    'public_order_id'=>1,
]);

the above code was not tested.

Menu