當我在瀏覽器上運行此代碼時,出現一條錯誤消息,指出存在未定義的索引。我花了幾個小時試圖讓它工作,但我只是不知道爲什麼我一直得到相同的確切的錯誤。未定義的索引錯誤
<?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>";
}
?>
如果您需要任何更多的細節,請在下面評論。
嘗試改變:'UPDATE tCompany SET'到:'UPDATE \'tCompany \''SET哪個 – alfasin
的代碼示例之一是companyUpdate.php?行號不匹配。 – dsas
在切線上,您的代碼使用已棄用的[mysql_fetch_array](http://www.php.net/mysql_fetch_array)編寫。這也是非常不安全的,你應該閱讀[SQL注入](http://stackoverflow.com/questions/60174/) – dsas