注意:未定义的索引:第13行的D:\ wamp \ www \ PDO \ sampleCRUD \ update.php中的省份?

时间:2015-06-27 01:09:38

标签: pdo

我这里有update.php的页面我的问题是我一直收到这个错误

Notice: Undefined index: province in D:\wamp\www\PDO\sampleCRUD\update.php on line 13

每次我在下拉列表中选择此值“---”时它会一直显示此错误,但如果在下拉列表中选择列表的其余部分则没有错误我尝试设置省但我仍然收到此错误。

这里是我的update.php代码

<?php
 include_once 'dbconfig.php';
  $username = isset($_GET['username']) ? $_GET['username'] : '';
  $password = isset($_GET['password']) ? $_GET['password'] : '';
  $province = isset($_GET['province']) ? $_GET['province'] : '';

   if(isset($_POST['btn-update']))
   {
   $user_id = $_GET['user_id'];
   $username = $_POST['username'];
   $password = $_POST['password'];
   $province = $_POST['province'];

   if($crud->update($user_id,$username,$password,$province))
   {
   echo "<script type='text/javascript'>alert('Record was updated Successfully!');</script>";
   }
   else
   {
   echo "<script type='text/javascript'>alert('Updating Failed!'); </script>";
   }
   }

   if(isset($_GET['user_id']))
   {
   $user_id = $_GET['user_id'];
   extract($crud->getID($user_id)); 
   }

   ?>

  <!DOCTYPE html>
  <html lang="en">
   <head>
     <meta http-equiv="Content-type" content="text/html; charset=UTF-8">
     <title>Survey Update Landholdings</title>
   <link rel="shortcut icon" href="image/icon.ico"/>
   <link rel="stylesheet" type="text/css" href="css/bootstrap.css">

  <div class="container">
   <p><strong>Survey Update</strong></p>
    <br />
     <div id="Survey-Update">
       <form method='post'>
        <table class='table table-bordered'>

          <tr>
            <td>First Name</td>
            <td><input type='text' name='username' class='form-control' value="<?php echo $username; ?>" required></td>
          </tr>

          <tr>
            <td>Last Name</td>
            <td><input type='text' name='password' class='form-control' value="<?php echo $password; ?>" required></td>
          </tr>

          <tr>
            <td>Province</td>
             <?php
                include_once 'dbconfig.php';

                $sql = "SELECT username FROM sample";
                $stmt = $DB_con->prepare($sql);
                $stmt->execute();
                $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
                    if ($stmt->rowCount() > 0)
                    {
                    ?>
             <td><select class='form-control' name='province'>
               <option selected="selected" disabled>---</option>
                <?php foreach ($results as $row)
                    {
                 ?>
                 <option value="<?php echo $row['username']; ?>"><?php echo $row['username']; ?></option>
                <?php
                    }
                ?>
                </select>

                <?php
                    }
                ?>
              </td>
            </tr>
            <tr>
             <td colspan="2">
              <button type="submit" class="btn btn-primary" name="btn-update">
              <span class="edit"></span>Update</button>
              <a href="index.php" onClick="self.close()"> &nbsp; CANCEL</a>
            </td>
          </tr>

       </table>
    </form>
  </div>

错误在此行

$province = $_POST['province'];

1 个答案:

答案 0 :(得分:0)

更改此行

<option selected="selected" disabled>---</option>

<option selected="selected" value="" disabled>---</option>

为省而不是没有发送空值。

编辑:上面的代码不起作用。

此处的问题是已禁用并已选中=&#34;已选中&#34;。看来,禁用选项无法提交。 如果你想坚持使用残疾人并选择我建议使用

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