-
-
Save josemalcher/7dbc039984115e7df04396dc84d8a986 to your computer and use it in GitHub Desktop.
Exemplo de como adicionar ou atualizar dados com WPDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
) | |
); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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