Skip to content

Instantly share code, notes, and snippets.

@josemalcher
Forked from claudiosanches/recebe.php
Created June 12, 2020 19:30
Show Gist options
  • Save josemalcher/7dbc039984115e7df04396dc84d8a986 to your computer and use it in GitHub Desktop.
Save josemalcher/7dbc039984115e7df04396dc84d8a986 to your computer and use it in GitHub Desktop.
Exemplo de como adicionar ou atualizar dados com WPDB
<?php
// Update table
function cs_add_data($competition, $date, $numbers) {
global $wpdb;
$table_name = $wpdb->prefix . 'nome da sua tabela';
$numbers = str_replace('/', '-', $numbers);
// Test variables
if (!is_numeric($competition))
wp_die('O número do concurso é inválido');
if (strlen($date) < 10)
wp_die('Inserá corretamente a data do concurso');
if (strlen($numbers) < 17)
wp_die('Inserá corretamente os números sorteados');
// Query the existence of row
$results = $wpdb->get_row("SELECT * FROM $table_name WHERE competition = '$competition'");
if ($results) {
// Upadate data
$wpdb->update($table_name, array(
'date' => $date,
'numbers' => $numbers
), array(
'ID' => $results->id
)
);
} else {
// Insert data
$wpdb->insert($table_name, array(
'competition' => $competition,
'date' => $date,
'numbers' => $numbers
)
);
}
}
?>
<?php
global $wpdb;
$table_name = $wpdb->prefix . "nome da sua tabela";
$sql = $wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", array(esc_attr($id)));
$results = $wpdb->get_results($sql);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment