$ _POST变量的值未显示到文本框

时间:2014-05-14 09:41:31

标签: php html

这不是我第一次使用post变量,但这个让我打破沉默,开玩笑, 无论如何,出现的错误是

注意:未定义的索引:第34行的C:\ xampp \ htdocs \ s1 \ agent.php中的AgentName

我的PHP代码

<?php
$AgentID = null;
if (!empty($_GET['AgentID'])) {
    $AgentID = $_REQUEST['AgentID'];
}

if (null==$AgentID) {
    header("Location: agent.php");
}

if (!empty($_POST)) {

    $AgentName = $_POST['AgentName'];
    $CurrentBalance = $_POST['CurrentBalance'];

    $valid = true;

    if ($valid) {
        $setsu = dbSetsuzoku();
        $setsu->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "UPDATE agentdb SET AgentName = ?, CurrentBalance=? WHERE AgentID = ?";
        $q = $setsu->prepare($sql);
        $q->execute(array($AgentName,$CurrentBalance,$AgentID));
        $setsu = null;
        header("Location: agent.php");
    }
} else {
    $setsu = dbSetsuzoku();
    $setsu->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * FROM agentdb where AgentID = ?";
    $q = $setsu->prepare($sql);
    $q->execute(array($AgentID));
    $data = $q->fetch(PDO::FETCH_ASSOC);
    $AgentID = $data['AgentID'];
    $AgentName = $_POST['AgentName'];
    $CurrentBalance = $data['CurrentBalance'];
    $setsu = null;
}

?>

HTML

<input name="AgentName" type="text" id="agentMeiKakushi" value="<?php echo !empty($AgentName)?$AgentName:'';?>">

什么可能是mising或什么?

3 个答案:

答案 0 :(得分:2)

应该是:

<?php echo (isset($AgentName))?$AgentName:'';?>

答案 1 :(得分:0)

你的表单应该有POST方法:

<form method="POST" ...></form>

答案 2 :(得分:0)

我发现你的问题是你的代码

if (!empty($_POST)) {

    $AgentName = $_POST['AgentName'];
    $CurrentBalance = $_POST['CurrentBalance'];

    $valid = true;

    if ($valid) {
        $setsu = dbSetsuzoku();
        $setsu->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "UPDATE agentdb SET AgentName = ?, CurrentBalance=? WHERE AgentID = ?";
        $q = $setsu->prepare($sql);
        $q->execute(array($AgentName,$CurrentBalance,$AgentID));
        $setsu = null;
        header("Location: agent.php");
    }
} else {
    $setsu = dbSetsuzoku();
    $setsu->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * FROM agentdb where AgentID = ?";
    $q = $setsu->prepare($sql);
    $q->execute(array($AgentID));
    $data = $q->fetch(PDO::FETCH_ASSOC);
    $AgentID = $data['AgentID'];
    $AgentName = $_POST['AgentName']; // <------ here 
    $CurrentBalance = $data['CurrentBalance'];
    $setsu = null;
}

如果您声明$ _post是否包含数据frist选项,否则,第二个(其他)

最后的第4行,你有$ agentNAme = post ....这里你没有$ _POST的访问权,如果你和$ data混淆了。