【问题标题】:How can I find the ID for the last submitted record如何找到最后提交记录的 ID
【发布时间】:2020-02-28 22:40:23
【问题描述】:

我在这里有一个插件,可以为我的 wordPress 网站注册用户,但现在我想在提交表单后更新刚刚注册的用户的 ID。

我正在尝试在 WordPress 上编辑一个插件,在它提交表单后,我应该获取最后插入的 ID 并更新刚刚添加的记录。

        <?php do_action( 'user_registration_form_registration_end', $form_id ); ?>
        <?php

           // Add refer 
           global $wpdb;

           $table_name  = $wpdb->prefix."users";
           $current_user = wp_get_current_user();
          // $lastid = $wpdb->insert_id;

           if(isset($_GET['ID'])){

               $where = array('ID' => $current_user);
               $subs = array('refID'=> $_GET['ref_id']);
               $wpdb->update($table_name, $subs, $where);
           } 


        ?>

    </form>

    <div style="clear:both"></div>
</div>

我希望它从 url 中获取一个值,然后更新数据库中的用户记录。

【问题讨论】:

标签: php wordpress


【解决方案1】:

您可以这样做,因为它会返回最后插入的行

$sql="SELECT * FROM `table-name` ORDER BY id DESC LIMIT 1";
$result=mysqli_fetch_array($sql);

【讨论】:

  • 还有:WHERE id=MAX(id) 或 id=LAST_INSERT_ID()
猜你喜欢
  • 2023-03-22
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
相关资源
最近更新 更多