add_post_meta() & update_post_meta()

add_post_meta() : Adds a meta field to the given post.

Below is the example,

<?php

$post_id = 1;
$meta_key = '_test';
$meta_value="this is a test";
$unique = true;

add_post_meta( $post_id, $meta_key, $meta_value, $unique ); 

update_post_meta() : if the given key already exists among custom fields of the specified post, another custom field with the same key is added unless the $unique argument is set to true, in which case, no changes are made. If you want to update the value of an existing key, use the update_post_meta() function

It will fire when a post type is saved or updated, several actions fire, any of which might be appropriate to hook into in order to save the entered values.

Below is the example,

function wporg_save_postdata( $post_id ) {
    if ( array_key_exists( 'wporg_field', $_POST ) ) {
        update_post_meta(
            $post_id,
            '_wporg_meta_key',
            $_POST['wporg_field']
        );
    }
}
add_action( 'save_post', 'wporg_save_postdata' );