繁体   English   中英

未定义的索引错误

[英]Undefined index errors

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

<?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>";
}

?>

如果您需要更多详细信息,请在下面评论。

您的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'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM