如何修复注意:PDO

时间:2017-05-18 07:39:48

标签: php mysql

我有

注意:未定义索引:C:\ xampp \ htdocs \ otakutangerang_admin \ edit_user.phpon第26行中的_ava

 $avatar = $_POST['_ava'];

注意:未定义的索引:第27行的C:\ xampp \ htdocs \ otakutangerang_admin \ edit_user.php中的_skill

$usr_skill = $_POST['_skill'];

这是我的代码

  if (isset($_POST['update'])) {

  $id = $_GET['id'];
  $email = $_POST['_em'];
  $fname = $_POST['_fn'];
  $lname = $_POST['_ln'];
  $web_usr = $_POST['_web'];
  $usr_note = $_POST['_note'];
  $avatar = $_POST['_ava'];
  $usr_skill = $_POST['_skill'];

  //Retrieve the user account information by id.
    if ($object->Update($id, $email, $fname, $lname, $web_usr, $usr_note, $avatar, $usr_skill)) {
      $msg = "<div class='alert alert-info'><strong>WOW!</strong> Record was updated successfully <a href='index.php'>HOME</a>!</div>";
    } else {
      $msg = "<div class='alert alert-warning'><strong>SORRY!</strong> ERROR while updating record !</div>";
    }
  }

  if (isset($_GET['id'])) {
    $id = $_GET['id'];
    extract($object->Details($id));
  }

这是我改变和编辑数据库的表格

    <form class="form-horizontal" method="POST">
      <div class="box-body">
        <div class="form-group">
        <input type="hidden" name="id" value="<?php echo $id; ?>">
          <label for="inputUserName" class="col-sm-2 control-label">Username</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="inputUserName" value="<?php echo $username; ?>" disabled>
            <p class="help-block"><em>You require permission from Administrators to change it.</em></p>
          </div>
        </div>
        <div class="form-group">
          <label for="inputFirstName" class="col-sm-2 control-label">First Name</label>
          <div class="col-sm-10">
            <input type="text" name="_fn" class="form-control" id="inputFirstName" value="<?php echo $fname; ?>">
          </div>
        </div>
        <div class="form-group">
          <label for="inputLastName" class="col-sm-2 control-label">Last Name</label>
          <div class="col-sm-10">
            <input type="text" name="_ln" class="form-control" id="inputLastName" value="<?php echo $lname; ?>">
          </div>
        </div>
        <div class="form-group">
          <label for="inputEmail" class="col-sm-2 control-label">Email</label>
          <div class="col-sm-10">
            <input type="email" name="_em" class="form-control" id="inputEmail" value="<?php echo $email; ?>">
          </div>
        </div>
        <div class="form-group">
          <label for="inputEmail" class="col-sm-2 control-label">Website</label>
          <div class="col-sm-10">
            <input type="text" name="_web" class="form-control" id="inputEmail" value="<?php echo $web_usr; ?>">
          </div>
        </div>
        <div class="form-group">
          <label for="inputEducation" class="col-sm-2 control-label">Education</label>
          <div class="col-sm-10">
            <input type="email" name="_edu" class="form-control" id="inputEducation" placeholder="Education">
          </div>
        </div>
        <div class="form-group">
          <label for="inputNote" class="col-sm-2 control-label">Notes</label>
          <div class="col-sm-10">
            <textarea class="form-control" id="inputNote" value="<?php echo $usr_note; ?>" placeholder="Note" name="_note"></textarea>
          </div>
        </div>
        <div class="form-group">
          <label for="inputSkill" class="col-sm-2 control-label">Skill</label>
          <div class="col-sm-10">
            <select class="form-control select2" name="_skill" value="<?php echo $usr_skill; ?>" multiple="multiple" data-placeholder="Skill" >
              <option value="Rasengan">Rasengan</option>
              <option value="Chidori">Chidori</option>
              <option value="Susano'o">Susano'o</option>
              <option value="Kamehameha">Kamehameha</option>
            </select>
          </div>
        </div>
        <div class="form-group">
          <label for="inputRole" class="col-sm-2 control-label">Role</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="inputRole" value="<?php echo $id_admrole; ?>" disabled>
            <p class="help-block"><em>You require permission from Administrators to change it.</em></p>
          </div>
        </div>
        <div class="form-group">
          <label for="inputFile3" class="col-sm-2 control-label">Foto Profil</label>
          <div class="col-sm-10">
            <div class="input-group">
              <label class="input-group-btn">
                <span class="btn btn-primary">
                  Browse&nbsp; <input type="file" style="display: none;" accept="image/*" multiple id="file">
                </span>
              </label>
              <input type="text" class="form-control" name="_ava" value="<?php echo $avatar; ?>" readonly disabled>
            </div>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" name="update" class="btn btn-danger" value="update">UPDATE</button>
          </div>
        </div>
      </div>
      <!-- /.box-body -->
    </form>

4 个答案:

答案 0 :(得分:0)

设置error_reporting时收到的这些注意事项也显示E_NOTICE。 但是,这是我认为的最佳实践(首先检查$ _POST是否存在以及是否存在数组键):

$avatar = isset($_POST) && array_key_exists('_ava', $_POST) ? $_POST['_ava'] : "";
$usr_skill = isset($_POST) && array_key_exists('_skill', $_POST) ? $_POST['_skill'] : "";

答案 1 :(得分:0)

将两行更改为:

<?php
if(isset($_POST['_ava']))
    $avatar = $_POST['_ava'];
else
    $avatar = null; //or any other default value

if(isset($_POST['_skill']))
    $usr_skill = $_POST['_skill'];
else
    $usr_skill = null; //or any other default value

通知出现是因为没有设置_skill或_ava。

答案 2 :(得分:0)

使用isset()

$avatar = isset($_POST['_ava']) ? $_POST['_ava'] : null;

$usr_skill = isset($_POST['_skill']) ? $_POST['_skill'] : null;

和其他post value一样。希望它会帮助你

了解更多信息

http://php.net/manual/en/function.isset.php

答案 3 :(得分:0)

您应该只使用readonly属性,而不是禁用。 Check this

  • readonly:输入无法修改
  • 已禁用:输入没有表单功能

并检查表单输入是否为isset,如下所示:

$avatar = null;
if(isset($_POST['_ava'])) {
   $avatar = $_POST['_ava'];
}