WP - 用户自己更新用户元 - 保存时遇到麻烦

时间:2017-10-01 18:10:47

标签: php wordpress forms

我有一个关于Wordpress update_user_meta的基本问题。我正在尝试编写一个from,可以由用户编辑。问题是,提交的表单中的值保存在数据库中,但重新加载页面后,从DB中删除了值。

代码是:

<?php
$current_user_id = get_current_user_id();
 echo $user->schoolstudentscount;
 ?>
<form name="Students Count" action="" method="POST">
<fieldset>
<input type="text" id="count_of_students" name="count_of_students"/>
</fieldset>
<button type="submit">Save</button>
 </form>
<?php
$low_price = $_POST['count_of_students'];
update_user_meta( $current_user->ID, 'schoolstudentscount', $low_price); 
 ?>

更好的解释:

  1. 简单形式,其中数字35取自DB。
  2. 值更改为25,表单已提交
  3. 值25存储在DB中并显示在值字段
  4. F5值从输入字段和数据库中消失后。
  5. SQL number taken from Database

    enter image description here

    enter image description here

    enter image description here

    如果我理解得很好,我需要以某种方式重定向页面本身以将数据存储在数据库中而不会被删除。

    任何人都知道如何正确编写程序?

    提前谢谢。

1 个答案:

答案 0 :(得分:0)

if ( isset( $_POST['count_of_students'] ) ){
    $low_price = $_POST['count_of_students'];
    update_user_meta( $current_user->ID, 'schoolstudentscount', $low_price);
}

如果提交/发布了count_of_students,则只会更新用户元。

相关问题