Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

414
Visualizações
How to get the result of $wpdb ->query to know if it was successful

I am currently working on a wordpress site,

I am using a form that the user can modify so that it updates the database :

Here's the HTML/PHP code :

echo '<form class="form-verifdoc" target="noredirect" method = "post">';
echo '<label class="label-verifdoc" style="float:left;margin-top:10px;margin-right:10px;"for="'.$name_input_verifdoc.'">'.$label_element.'</label><textarea style="float:right;margin-bottom:10px;vertical-align:middle!important;border-left: 5px solid black;resize:vertical;" rows="1" type="text" name="variables['.$name_input_verifdoc.']">'.$contenu_input_verifdoc.'</textarea>';
echo'<input name="idchamps['.$id_input_verifdoc.']" type="hidden" value="'.$id_input_verifdoc.'">';
echo '<div class="div_edit_verifdoc">';
echo '<input type="submit" id="'.$id_input_verifdoc.'" class="bouton-edit-champ-doc" onclick="return confirm(\'Voulez-vous vraiment modifier les informations de ce champ ?\') && AlertQuery()" name="bouton-envoi-editform" value="Editer le champ"/>';
echo '</div>';
echo '<iframe name="noredirect" style="display:none;"></iframe>';
echo '</form>';

I use a confirm so that the user has to confirm that he wants to update the data.

And here's the script that use the wpdb query to update the MySQL data :

 <script>
<?php

 if (isset($_POST['bouton-envoi-editform']) && !empty($_POST['bouton-envoi-editform'])) {

   foreach($_POST['variables'] as $variableName => $variable){
     $ValeurChampModif = $variable;
   }

   foreach($_POST['idchamps'] as $champid => $idchamp){
     $Champ_id = $idchamp;
   }

$modif_doc_bdd = $wpdb->query($wpdb->prepare("UPDATE `mod114_frmt_form_entry_meta_copie` SET  `meta_value` = '$ValeurChampModif' WHERE `mod114_frmt_form_entry_meta_copie`.`meta_id` =    $Champ_id;"));

} else {

}

?> 

function AlertQuery($modif_doc_bdd){

  <?php
  if( $modif_doc_bdd ){
    ?>
    alert('Les modifications ont bien été enregistrées.');
  <?php
  }else{
    ?>
    alert('⚠ ERREUR, les modifications n\'ont pas pu être enregistrées. Veuillez ré-essayer.');
  <?php
  }
  ?>
}
    </script>

I am trying to get the function AlertQuery to make a popup alert if the wpdb query was successful or not when updating the data.

But I can't seem to grab the response of the $wpdb query...

I have tried to put the function to have everything inside but that didn't work as it wouldn't prompt the popup.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you use $wpdb->update() it returns the number of rows updated on success or false on error , if you then check if $wpdb->update() is false or not you can call your AlertQuery() method accordingly.

I hope I kind of explained properly, see docs here: https://developer.wordpress.org/reference/classes/wpdb/update/#return

about 4 years ago · Juan Pablo Isaza Relatório

0

$modif_doc_bdd = $wpdb->query($wpdb->prepare("UPDATE `mod114_frmt_form_entry_meta_copie` SET  `meta_value` = '$ValeurChampModif' WHERE `mod114_frmt_form_entry_meta_copie`.`meta_id` =    $Champ_id;"));
 
if ( false === $modif_doc_bdd ) {
    // There was an error.
} else {
    // No error. You can check updated to see how many rows were changed.
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda