Php 尝试更新wordpress用户元数据时出现两个问题

Php 尝试更新wordpress用户元数据时出现两个问题,php,wordpress,forms,post,switch-statement,Php,Wordpress,Forms,Post,Switch Statement,我有一个表单,它向php脚本提交数据。下面是脚本中的一些代码 foreach ($newuser_values as $key => $value) { echo $key; echo $value; switch ($key) { case "show_name": echo "1"; switch ($value) {

我有一个表单,它向php脚本提交数据。下面是脚本中的一些代码

foreach ($newuser_values as $key => $value) {
        echo $key;
        echo $value;
            switch ($key) {
                case "show_name":
                    echo "1";
                    switch ($value) {                       
                        case "first":
                        $update_val = $current_user->user_firstname;
                        break;
                        case "first_initial":
                        echo "HI";
                        $update_val = $current_user->user_firstname . " " . substr($current_user->user_lastname,0,0) . ".";
                        break;
                        case "first_last":
                        $update_val = $current_user->user_firstname . " " . $current_user->user_lastname;
                        break;
                        default:
                        echo "HELLO";
                        $update_val = $user_identity;
                    }
                    echo $update_val;
                    update_user_meta($user_ID, $show_name, $update_val);
                    echo get_user_meta($user_ID, $show_name);
                    ...
             }
        }
$newuser_values是表单信息的数组。我是这样理解的:

$newuser_values = $_POST['edit-profile'];
我这里有两个问题。它们都可以从echo语句的脚本输出的开头看到。脚本输出为:

显示第一个姓名第一个初始HelloteStuer1阵列

问题1:如您所见,脚本打印一个1,这意味着它正在输入名为show_name of the switch on$key的案例。但是,它没有输入名为“打开$value开关的第一个初始值”的案例。为什么会这样

问题2:无论如何,脚本进入默认情况下的$value开关并打印HELLO。然后,它打印$update_val,它是testuser1。但是,该值没有分配给用户_meta,因为它正在打印数组。为什么会这样


提前谢谢。如果这些问题过于简单,我深表歉意。我对wordpress和web开发相当陌生。

看来您第一期的原因是打字错误。你写了你的脚本打印出来

显示第一个姓名第一个初始HelloteStuer1阵列


因此,在检查开关盒时,$value的值是first_inital而不是first_initial,这似乎是一个输入错误。

没关系,我解决了第二个问题

与此相反:

update_user_meta($user_ID, $show_name, $update_val);
应该是这样的:

update_user_meta($user_ID, "show_name", $update_val);

$user\u identity似乎是一个数组,但您的代码没有显示声明它的位置。没有,因为我有echo$update\u val语句。这将导致testuser1,它只是一个stringget\u user\u meta$user\u ID,$show\u name将返回一个数组,除非您传入第三个参数true。尝试打印目标用户元$user\u ID,$show\u name;而不是echo,并查看数组值是什么。或者尝试打印目标用户元$user\u ID,$show\u name,true;要检查单个返回值。print\r get\u user\u meta$user\u ID,$show\u name,true提供:Array[first\u name]=>Array[0]=>hank[last\u name]=>Array[0]=>collins[昵称]=>Array[0]=>testuser3[description]=>Array[0]=>true[comment\u-shortcuts]=>Array[0]=>false[admin\u颜色]=>数组[0]=>fresh[使用ssl]=>Array[0]=>0[显示管理栏前面]=>Array[0]=>true[wp\u功能]=>Array[0]=>a:1:{s:6:作者;b:1;}。。。