Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

408
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!