最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

database - getting the values of hidden inputs to use them in a php mysql query

matteradmin10PV0评论

Im trying to update a value in the database, to do this I pass the values of the input an ID to another hidden inputs and submit the form via jquery, the values are set in the hidden input (tested). But I cant get them to pass to PHP and perform the query, what Im doing wrong?

  <?php  
  global $wpdb;
  $results = $wpdb->get_results( "SELECT * FROM $wpdb->users" ) ?>

  <form id="form1">
      <?php foreach($results as $key): ?>
          <?php echo $key->ID.$key->display_name.$key->mg_nobility; ?>
          <!-- Append the id to the end of this -->
          <input type='text' id='edit-value-<?php echo $key->ID ?>' value='' /> 
          <!-- Use the data attr instead -->
          <button data-rowid='<?php echo $key->ID ?>' class='edit_nov'>Submit</button><br>
      <?php endforeach ?>
  </form>

  <input type='hidden' name='del_id' id='row_del_id' value=''>
  <input type='hidden' name='up_id' id='row_up_id' value=''>

  <script>
  jQuery('.edit_nov').click(function($) {
      var current_id = jQuery(this).data('rowid');
      var current_value = jQuery('#edit-value-'+current_id).val();
      jQuery('#row_del_id').val(current_id);
      jQuery('#row_up_id').val(current_value);
      jQuery( "#form1" ).submit();
      return false;
  });
  </script>

  <?php 
  if(isset($_POST['del_id']))
  {
    $id = $_POST['del_id'];
    $nobility = $_POST['up_id'];
    global $wpdb;  
    $wpdb->query($wpdb->prepare("UPDATE $wpdb->users SET mg_nobility='$nobility' WHERE ID='$id'"));
  }
  ?>

Im trying to update a value in the database, to do this I pass the values of the input an ID to another hidden inputs and submit the form via jquery, the values are set in the hidden input (tested). But I cant get them to pass to PHP and perform the query, what Im doing wrong?

  <?php  
  global $wpdb;
  $results = $wpdb->get_results( "SELECT * FROM $wpdb->users" ) ?>

  <form id="form1">
      <?php foreach($results as $key): ?>
          <?php echo $key->ID.$key->display_name.$key->mg_nobility; ?>
          <!-- Append the id to the end of this -->
          <input type='text' id='edit-value-<?php echo $key->ID ?>' value='' /> 
          <!-- Use the data attr instead -->
          <button data-rowid='<?php echo $key->ID ?>' class='edit_nov'>Submit</button><br>
      <?php endforeach ?>
  </form>

  <input type='hidden' name='del_id' id='row_del_id' value=''>
  <input type='hidden' name='up_id' id='row_up_id' value=''>

  <script>
  jQuery('.edit_nov').click(function($) {
      var current_id = jQuery(this).data('rowid');
      var current_value = jQuery('#edit-value-'+current_id).val();
      jQuery('#row_del_id').val(current_id);
      jQuery('#row_up_id').val(current_value);
      jQuery( "#form1" ).submit();
      return false;
  });
  </script>

  <?php 
  if(isset($_POST['del_id']))
  {
    $id = $_POST['del_id'];
    $nobility = $_POST['up_id'];
    global $wpdb;  
    $wpdb->query($wpdb->prepare("UPDATE $wpdb->users SET mg_nobility='$nobility' WHERE ID='$id'"));
  }
  ?>
Share Improve this question asked Dec 6, 2018 at 13:16 Mefistofeles Mefistofeles 51 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Please add method="post" to form like

    <form id="form1" method="post">
          <?php foreach($results as $key): ?>
              <?php echo $key->ID.$key->display_name.$key->mg_nobility; ?>
              <!-- Append the id to the end of this -->
              <input type='text' id='edit-value-<?php echo $key->ID ?>' value='' /> 
              <!-- Use the data attr instead -->
              <button data-rowid='<?php echo $key->ID ?>' class='edit_nov'>Submit</button><br>
          <?php endforeach ?>
  <input type='hidden' name='del_id' id='row_del_id' value=''>
  <input type='hidden' name='up_id' id='row_up_id' value=''>
      </form>
Post a comment

comment list (0)

  1. No comments so far