获取隐藏输入的值以在php MySQL查询中使用它们

时间:2018-12-06 作者:Mefistofeles

我试图更新数据库中的一个值,为此,我将输入的值和ID传递给另一个隐藏输入,并通过jquery提交表单,这些值在隐藏输入中设置(测试)。但是我无法让他们传递到PHP并执行查询,我做错了什么?

  <?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\'"));
  }
  ?>

1 个回复
最合适的回答,由SO网友:Pratik Patel 整理而成

请添加method="post" 形成类似

    <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>