按下更新按钮时未定义的索引错误?

时间:2016-06-21 06:40:19

标签: php html

我在这里有一个选择选项,如果我更改选项的值,但是当我尝试不更改任何内容并且按下更新按钮时它会给我一个undefined error的错误。但如果我更改了一个选项的值,它就会成功更新。我真的不知道为什么它给我这个错误..

我的代码

<?php
include_once 'db.php';


$Reason_for_Deduction = isset($_GET['Summary_of_Reason']) ?      $_GET['Reason_for_Deduction'] : '';

if(isset($_POST['update']))
    {
    $ID = $_GET['ID'];
    $Reason_for_Deduction = $_POST['Summary_of_Reason'];

    if($LTID->update($ID,$Summary_of_Reason))
    {
    echo "<script type='text/javascript'>alert('Successfully Updated!');</script>";
    }
    else
    {
    echo "<script type='text/javascript'>alert('Updating Failed!');</script>";
    }
}
    if(isset($_GET['ID']))
    {
    $ID = $_GET['ID'];
    extract($LTID->getID($ID)); 
}
?>

update.php

<select name="Summary_of_Reason" id="yearapproved" class=form-control required/>
       <option selected="true" disabled="disabled" <?php echo $Summary_of_Reason; ?>><?php echo $Summary_of_Reason; ?></option>
       <option value="18% SLOPE ABOVE AND UNDEVELOPED">18% Slope and Undeveloped</option>
       <option value="FIVE (5) HAS. AND BELOW">5 Has. and Below</option>
       <option value="CANCELLED TITLE">Cancelled Title</option>
       <option value="CANNOT BE LOCATED ON THE GROUND">Cannot Be Located on the Ground</option>
       <option value="COMMUNAL FOREST">Communal Forest</option>
       <option value="DISTRIBUTED BEFORE CARPER">Distributed Before Carper</option>
       <option value="DUPLICATE LH">Duplicate Lh</option>
       <option value="ERODED; SILTED/ROCKY NOT SUITABLE TO AGRICULTURAL">Eroded;Silted/Rockt not suitable to Agricultural</option>
       <option value="HANDOG TITULO">Handog Titulo</option>
       <option value="HOMESTEAD PATENT">Homestead Patent</option>
       <option value="IRRIGATION CANAL">Irrigation Canal</option>
       <option value="LANDS FOR PUBLIC USE">Lands for Public Use</option>
       <option value="LH IS W/N SWAMPY, MANGROVE AREA">Lh is w/n Swampy, Mangrove Area</option>
       <option value="LO DIED PRIOR TO CARP">LO Died Prior to CARP</option>
       <option value="PASTURE LAND">Pasture Land</option>
       <option value="ROAD LOT">Road Lot</option>
       <option value="TIMBERLAND">Timberland</option>
       <option value="USED FOR INFRASTRUCTURE">Used for Infrastructure</option>
       <option value="W/N PROCLAIMED AREA">w/n Proclaimed Area</option>
       <option value="W/N DANGER ZONE">w/n Danger Zone</option>
       <option value="W/N UNCLASSIFIED PUBLIC FOREST">w/n Unclassified Public Forest</option>
       <option value="W/N WATERSHED AREA">w/n Watershed Area</option>
       <option value="WORKABLE">Workable</option>
</select>

class.php

public function update($ID,$Summary_of_Reason)
{
try
{
$stmt=$this->db->prepare("UPDATE rlbet SET Summary_of_Reason = :Summary_of_Reason WHERE ID = :ID");

$stmt->bindparam(":Summary_of_Reason",$Summary_of_Reason);

$stmt->bindparam(":ID",$ID);
$stmt->execute();

  return true;
}
catch(PDOException $e)
{
  echo $e->getMessage();
  return false;
}
}

2 个答案:

答案 0 :(得分:1)

您应该更改此行:

<option selected="true" disabled="disabled" <?php echo $Summary_of_Reason; ?>><?php echo $Summary_of_Reason; ?></option>

为:

<option selected="selected" disabled="disabled" value="<?php echo $Summary_of_Reason; ?>"><?php echo $Summary_of_Reason; ?></option>

您回复了$Summary_of_Reason inside选项标记的值,而不是其值,主要问题是selected = "true"需要更改为selected="selected"

答案 1 :(得分:0)

首先你必须从中删除/

你的选择框名称也是“Summary_of_Reason”,那么为什么你在动作页面中使用$ _GET [“Reason_for_Deduction”],你应该使用$ _GET [“Summary_of_Reason”]。 “Reason_for_Deduction”在表单中不可用,这就是它为您提供未定义索引错误的原因。