Prepared Statement with ON DUPLICATE KEY

前端 未结 2 1556
执念已碎
执念已碎 2020-12-17 15:58

I think this one should be pretty easy but I am having trouble getting it right. I have searched a bit but being new to prepared statements I can\'t quite figure out the sy

2条回答
  •  眼角桃花
    2020-12-17 16:22

    I'd strongly suggest having a look at something like Doctrine DBAL (not ORM) - it allows you to heave key => value pairs and makes these types of operations easier to wield when there's so many values.

    You can then do something like:

    try {
        $conn->insert(
        'db.`table`',
         [
           'city' => $city,
           'state' => $state
         ]);
    } catch (Exception $e) {
    
        if( $e->getCode() !== '23000' ) {
            throw $e;
        }
    
        $conn->update(
        'db.`table`',
         [
           'city' => $city,
           'state' => $state
         ],
         [
           'user' => $user
         ]);
    }
    

提交回复
热议问题