Home > OS >  How to get the result of $wpdb ->query to know if it was successful
How to get the result of $wpdb ->query to know if it was successful

Time:03-26

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  target="noredirect" method = "post">';
echo '<label  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 >';
echo '<input type="submit" id="'.$id_input_verifdoc.'"  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.

CodePudding user response:

$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.
}

CodePudding user response:

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

  • Related