我有一个提交数据到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语句中)。脚本输出如下:
show_namefirst_intital1HELLOtestuser1Array
问题1:正如您所看到的,脚本打印了一个1,这意味着它正在进入$key开关的标题为"show_name"的大小写。但是,它不进入$value开关的标题为"first_initial"的情况。为什么会这样?
问题2:无论如何,脚本进入$value开关的默认情况并打印HELLO。然后,它输出$update_val,即testuser1。然而,该值没有被分配给user_meta,因为它正在打印"Array"。为什么会这样?
提前感谢。如果这些问题过于简单化,我很抱歉。我对wordpress和web开发非常陌生。
看来你第一个问题的原因是打字错误。你写的脚本打印
show_namefirst_intital1HELLOtestuser1Array
所以似乎有一个错别字,因为$value的值是"first_initial"而不是"first_initial",因为你检查你的开关箱
没关系,我解决了第二个问题。
而不是:
update_user_meta($user_ID, $show_name, $update_val);
应该是这样的:
update_user_meta($user_ID, "show_name", $update_val);