Wordpress php未定义索引,表单发布到db

时间:2015-12-27 23:23:36

标签: php mysql wordpress forms

我正在为wordpress小部件构建一个非常小的表单。表单只是一个下拉选择输入,允许用户选择他们的业务类型 - 然后将其存储在数据库的wp_usermeta表中。

下拉列表实际上是在注册时选择的,因此这种形式更有可能让用户更改价值。

这是我用来创建表单的php:

<?php 
$user = wp_get_current_user();        
$selected = get_user_meta( $user->ID, 'i_am_a', true ); 
?>

<form method="post">        
<h3>I am a...</h3>
        <select name="i_am_a" id="i_am_a">
            <option value="musician" autocomplete="off" <?php echo ($selected == "musician")?  'selected="selected"' : '' ?>>Musician/Artist</option>
            <option value="band" autocomplete="off" <?php echo ($selected == "band")?  'selected="selected"' : '' ?>>Band</option>
            <option value="photographer" autocomplete="off" <?php echo ($selected == "photographer")?  'selected="selected"' : '' ?>>Photographer</option>
            <option value="business" autocomplete="off" <?php echo ($selected == "business")?  'selected="selected"' : '' ?>>Small Business</option>
            <option value="other" autocomplete="off" <?php echo ($selected == "other")?  'selected="selected"' : '' ?>>Other</option>
        </select>
<input type="submit" name="submit" value="change" class="button"/>

</form>
<?php
    $i_am_a = $_POST['i_am_a'];
    update_user_meta( $user->ID, 'i_am_a', $i_am_a );
?>

注意:未定义的索引:第90行的/home/.../layers-whitelabel.php中的i_am_a

第90行如下:

$i_am_a = $_POST['i_am_a'];

如何摆脱这个错误?

2 个答案:

答案 0 :(得分:1)

声明你的变量。或者使用isset()检查它们是否在引用它们之前声明,如:

$i_am_a = isset($_POST['i_am_a']) ? $_POST['i_am_a'] : '';

答案 1 :(得分:0)

试试这个,

  $i_am_a = isset($_POST['i_am_a']) ? $i_am_a = $_POST['i_am_a'] : 0;

有关使用0进行检查的说明,请参阅this answer