Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Created September 30, 2012 18:49
Show Gist options
  • Save claudiosanches/3808127 to your computer and use it in GitHub Desktop.
Save claudiosanches/3808127 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