WORDPRESS:创建一个在点击时更新数据库的功能

I have created a function but I have come to a hurdle, I want to make the if/else hrefs clickable and I need them to add/remove entries into the database, whats the best way to do this?

    // FAVORITE
function favorite($user_id, $post_id) {

    // DB Connection
    $mysqli = mysqli_connect("localhost", "???", "???", "???");

    // Check Favorite
    $getFavoriteQuery = "
        SELECT  *
        FROM    wp_favorites
        WHERE   user_id = $user_id
        AND     post_id = $post_id";

    $favoriteQuery = $mysqli->query($getFavoriteQuery); 

    if (mysqli_num_rows($favoriteQuery)) { ?>

        <a href="#">Un-favorite</a>

        <!-- UPDATE DATABASE MYSQLI QUERY -->

    <?php } else { ?>

        <a href="#">Favorite</a>

        <!-- UPDATE DATABASE MYSQLI QUERY -->

    <?php }

}
?>

This is not the best way necessarily, it certainly isn't the cleanest but it should give you an idea of which direction to head in. Also this is (I think) pretty much what @iSS meant. So credit.

Front End

function isUserFavorite($user_id, $post_id) {

    // DB Connection
    $mysqli = mysqli_connect("localhost", "???", "???", "???");

    // Check Favorite
    $getFavoriteQuery = "
        SELECT  *
        FROM    wp_favorites
        WHERE   user_id = $user_id
        AND     post_id = $post_id";

    $favoriteQuery = $mysqli->query($getFavoriteQuery); 

    if (mysqli_num_rows($favoriteQuery))
        return true
    else 
        return false
}

if(isUserFavorite($user, $post)){
    $url = "http://domain.com/some/page/with/processing.php?action=unfavorite&user=$user&post=$post";
    $link = "UnFavorite";
}
else{
    $url = "http://domain.com/some/page/with/processing.php?action=favorite&user=$user&post=$post";
    $link = "Favorite";

<a href="<?php echo $url"><?php echo $link ?></a>

Processing.php

<?php
if(isset($_GET['action'],$_GET['user'],$_GET['post']){
    //MySQL Connect

    if($_GET['action']=='favorite'){
        //Query to favorite
    } 
    else if ($_GET['action']=='unfavorite'){
        //Query to unfavorite
    }
    //Redirect here if you like
}
else
    echo "Missing parameter"