function update_page_table($dbh, $page_name, $cnt_seq, $is_active)
{
    $query = "SELECT pagecounter FROM pagina ORDER BY pagecounter DESC LIMIT 1;";
    $sth = $dbh->prepare($query);
    $sth->execute();
    if ($sth->rowCount()) {
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        $page_cnt = $row['pagecounter'] + 1;
    } else {
        // Table is empty, use id 1
        $page_cnt = 1;
    }
    $query = "INSERT INTO pagina (userid, pagecounter, nome, idseq, ativa) VALUES (?, ?, ?, ?, ?);";
    $sth = $dbh->prepare($query);
    $sth->execute(array(get_logged_in_userid(), $page_cnt, $page_name, $cnt_seq, $is_active));
}
function update_register_table($dbh, $reg_name, $cnt_seq, $is_active, $typecont)
{
    $query = "SELECT regcounter FROM registo ORDER BY regcounter DESC LIMIT 1;";
    $sth = $dbh->prepare($query);
    $sth->execute();
    if ($sth->rowCount()) {
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        $reg_cnt = $row['regcounter'] + 1;
    } else {
        // Table is empty, use id 1
        $reg_cnt = 1;
    }
    $query = "INSERT INTO registo (userid,typecounter, regcounter, nome, idseq, ativo) VALUES (?,?, ?, ?, ?, 1);";
    $sth = $dbh->prepare($query);
    $sth->execute(array(get_logged_in_userid(), $typecont, $reg_cnt, $reg_name, $cnt_seq));
}
function update_field_table($dbh, $field_name, $cnt_seq, $is_active, $typecont)
{
    $query = "SELECT campocnt FROM campo WHERE userid=? ORDER BY campocnt DESC LIMIT 1;";
    $sth = $dbh->prepare($query);
    $sth->execute(array(get_logged_in_userid()));
    if ($sth->rowCount()) {
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        $field_cnt = $row['regcounter'] + 1;
    } else {
        // Table is empty, use id 1
        $field_cnt = 1;
    }
    $query = "INSERT INTO campo (userid,typecnt, campocnt, nome, idseq, ativo) VALUES (?,?, ?, ?, ?, 1);";
    $sth = $dbh->prepare($query);
    $sth->execute(array(get_logged_in_userid(), $typecont, $field_cnt, $field_name, $cnt_seq));
}
function update_reg_type_table($dbh, $reg_type_name, $cnt_seq, $is_active)
{
    $query = "SELECT typecnt FROM tipo_registo ORDER BY typecnt DESC LIMIT 1;";
    $sth = $dbh->prepare($query);
    $sth->execute();
    if ($sth->rowCount()) {
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        $typecnt = $row['typecnt'] + 1;
    } else {
        // Table is empty, use id 1
        $typecnt = 1;
    }
    $query = "INSERT INTO tipo_registo (userid, typecnt, nome, ativo, idseq, ptypecnt) VALUES (?, ?, ?, ?, ?, ?);";
    $sth = $dbh->prepare($query);
    $sth->execute(array(get_logged_in_userid(), $typecnt, $reg_type_name, $is_active, $cnt_seq, NULL));
}
function clone_campo($dbh, $typecnt, $campocnt)
{
    $orig_idseq = null;
    // Get values to clone
    $query = 'SELECT userid, typecnt, campocnt, idseq, ativo, nome, pcampocnt FROM campo
            WHERE userid = ? AND typecnt = ? AND campocnt = ? LIMIT 1;';
    $sth = $dbh->prepare($query);
    $sth->execute(array(get_logged_in_userid(), $typecnt, $campocnt));
    if ($sth->rowCount()) {
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        $userid = $row['userid'];
        $typecnt = $row['typecnt'];
        $campocnt = $row['campocnt'];
        $orig_idseq = $idseq = $row['idseq'];
        $ativo = $row['ativo'];
        $nome = $row['nome'];
        $pcampocnt = $row['pcampocnt'];
    }
    $query = 'INSERT INTO campo (userid, typecnt, campocnt, idseq, ativo, nome, pcampocnt) VALUES (?, ?, ?, ?, ?, ?, ?);';
    $sth = $dbh->prepare($query);
    $new_campocnt = get_new_campo_campocnt($dbh);
    $sth->execute(array($userid, $typecnt, $new_campocnt, $idseq, 0, $nome, $pcampocnt));
    return array('userid' => $userid, 'typecnt' => $typecnt, 'campocnt' => $campocnt, 'ativo' => $ativo, 'pcampocnt' => $new_campocnt);
}
function update_sequencia_table($dbh)
{
    $query = "SELECT contador_sequencia FROM sequencia ORDER BY contador_sequencia DESC LIMIT 1";
    $sth = $dbh->prepare($query);
    $sth->execute();
    if ($sth->rowCount()) {
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        $cnt_seq = $row['contador_sequencia'] + 1;
    } else {
        // Table empty, use id 1
        $cnt_seq = 1;
    }
    $query = "INSERT INTO sequencia(contador_sequencia, moment, userid) VALUES(?, ?, ?);";
    $timestamp = get_curr_timestamp();
    $userid = get_logged_in_userid();
    // TODO: null check
    $sth = $dbh->prepare($query);
    $sth->execute(array($cnt_seq, $timestamp, $userid));
    return $cnt_seq;
}