未定义的索引错误

时间:2015-07-21 22:33:02

标签: php undefined-index

当我在浏览器上运行此代码时,我收到一条错误消息,指出存在未定义的索引。我花了几个小时试图让它工作,但我只是不知道为什么我一直得到同样的错误。

<?php
if ($dbSuccess) {
$companyID = $_POST['ID'];

$preName = $_POST['preName'];
$companyName = $_POST['Name'];
$RegType = $_POST['RegType'];
$StreetA = $_POST['StreetA'];
$StreetB = $_POST['StreetB'];
$StreetC = $_POST['StreetC'];
$Town = $_POST['Town'];
$County = $_POST['County'];
$Postcode = $_POST['Postcode'];
$COUNTRY = $_POST['COUNTRY'];

}

$tCompany_SQLupdate = "UPDATE tCompany SET ";
$tCompany_SQLupdate .= "preName = ".$preName.", ";
$tCompany_SQLupdate .= "Name = ".$companyName.", ";
$tCompany_SQLupdate .= "RegType = ".$RegType.", ";
$tCompany_SQLupdate .= "StreetA = ".$StreetA.", ";
$tCompany_SQLupdate .= "StreetB = ".$StreetB.", ";
$tCompany_SQLupdate .= "StreetC = ".$StreetC.", ";
$tCompany_SQLupdate .= "Town = ".$Town.", ";
$tCompany_SQLupdate .= "County = ".$County.", ";
$tCompany_SQLupdate .= "Postcode = ".$Postcode.", ";
$tCompany_SQLupdate .= "COUNTRY = ".$COUNTRY.", ";
$tCompany_SQLupdate .= "WHERE ID = ".$companyID." ";

if (empty($companyName)) {
    echo '<span style="color: red;">Cannot make the company name empty.</span><br /><br />';
}   else {
    echo '<span style="text-decoration: underline;">
    SQL statement</span>
    <br />'.$tCompany_SQLupdate.'<br /><br />';

    if (mysql_query($tCompany_SQLupdate)) {
        echo 'used to Successfully update the company.<br /><br />';
    }   else {
        echo '<span style="color: red;">FAILED to update the company.</span><br /><br />';
    }
}

?>

错误讯息:

Notice: Undefined index: ID in C:\xampp\htdocs\forms\companyUpdate.php on line 40

Notice: Undefined index: preName in C:\xampp\htdocs\forms\companyUpdate.php on line 42

Notice: Undefined index: Name in C:\xampp\htdocs\forms\companyUpdate.php on line 43

Notice: Undefined index: RegType in C:\xampp\htdocs\forms\companyUpdate.php on line 44

Notice: Undefined index: StreetA in C:\xampp\htdocs\forms\companyUpdate.php on line 45

Notice: Undefined index: StreetB in C:\xampp\htdocs\forms\companyUpdate.php on line 46

Notice: Undefined index: StreetC in C:\xampp\htdocs\forms\companyUpdate.php on line 47

Notice: Undefined index: Town in C:\xampp\htdocs\forms\companyUpdate.php on line 48

Notice: Undefined index: County in C:\xampp\htdocs\forms\companyUpdate.php on line 49

Notice: Undefined index: Postcode in C:\xampp\htdocs\forms\companyUpdate.php on line 50

Notice: Undefined index: COUNTRY in C:\xampp\htdocs\forms\companyUpdate.php on line 51

形式:

<?php
if ($dbSuccess) {
$companyID = $_POST['companyID'];

$tCompany_SQLselect = "SELECT * ";
$tCompany_SQLselect .= "FROM ";
$tCompany_SQLselect .= "tCompany ";
$tCompany_SQLselect .= "WHERE ID = ".$companyID." ";

$tCompany_SQLselect_Query = mysql_query($tCompany_SQLselect);

while ($row = mysql_fetch_array($tCompany_SQLselect_Query, MYSQL_ASSOC)) {
    $current_preName = $row['preName'];
    $current_Name = $row['Name'];
    $current_RegType = $row['RegType'];
    $current_StreetA = $row['StreetA'];
    $current_StreetB = $row['StreetB'];
    $current_StreetC = $row['StreetC'];
    $current_Town = $row['Town'];
    $current_County = $row['County'];
    $current_Postcode = $row['Postcode'];
    $current_COUNTRY = $row['COUNTRY'];
}

echo '<h2 style="font-family: arial, helvetica, sans-serif;">
        Company EDIT form
        </h2>';

echo '<form name="postCompany" action="companyUpdate.php" method="post">';

echo '<input type="hidden" name="companyID" value="'.$companyID.'">';
echo '
    <table>
        <tr>
            <td>pre Name</td>
            <td><input type="text" name="" value="'.$current_preName.'"></td>
        </tr>
        <tr>
            <td>Name</td>
            <td><input type="text" name="" value="'.$current_Name.'"></td>
        </tr>
        <tr>
            <td>Reg Type</td>
            <td><input type="text" name="" value="'.$current_RegType.'"></td>
        </tr>
        <tr>
            <td>Street A</td>
            <td><input type="text" name="" value="'.$current_StreetA.'"></td>
        </tr>
        <tr>
            <td>Street B</td>
            <td><input type="text" name="" value="'.$current_StreetB.'"></td>
        </tr>
        <tr>
            <td>Street C</td>
            <td><input type="text" name="" value="'.$current_StreetC.'"></td>
        </tr>
        <tr>
            <td>Town</td>
            <td><input type="text" name="" value="'.$current_Town.'"></td>
        </tr>
        <tr>
            <td>County</td>
            <td><input type="text" name="" value="'.$current_County.'"></td>
        </tr>
        <tr>
            <td>Postcode</td>
            <td><input type="text" name="" value="'.$current_Postcode.'"></td>
        </tr>
        <tr>
            <td>COUNTRY</td>
            <td><input type="text" name="" value="'.$current_COUNTRY.'"></td>
        </tr>
        <tr>
            <td></td>
            <td align="right"><button type="submit">Save</button></td>
        </tr>
    </table>
';

echo "</form>";
}

?>

如果您需要更多详情,请在下面发表评论。

1 个答案:

答案 0 :(得分:0)

您的HTML表单字段需要name属性设置是错误的,因为您没有设置它们,它们将在$ _POST中以数字方式编入索引,而不是通过名称索引,使用print_r($ _ POST)来查看我的意思。

您需要更新HTML以将name =“”设置为name =“COUNTRY”等。

您可能还有一个问题,即数据库中的列名都是小写的(通常情况下是imo),并且您在第二个代码示例中的不同情况下访问它们。

尝试使用print_r检查while循环中$ row包含的内容。您可能会发现需要更改为:

$current_preName = $row['prename']; $current_Name = $row['name']; $current_RegType = $row['regtype']; $current_StreetA = $row['streeta']; $current_StreetB = $row['streetb']; $current_StreetC = $row['streetc']; $current_Town = $row['town']; $current_County = $row['county']; $current_Postcode = $row['postcode']; $current_COUNTRY = $row['country'];