2015-07-21 88 views
1

當我在瀏覽器上運行此代碼時,出現一條錯誤消息,指出存在未定義的索引。我花了幾個小時試圖讓它工作,但我只是不知道爲什麼我一直得到相同的確切的錯誤。未定義的索引錯誤

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

?> 

如果您需要任何更多的細節,請在下面評論。

+0

嘗試改變:'UPDATE tCompany SET'到:'UPDATE \'tCompany \''SET哪個 – alfasin

+0

的代碼示例之一是companyUpdate.php?行號不匹配。 – dsas

+0

在切線上,您的代碼使用已棄用的[mysql_fetch_array](http://www.php.net/mysql_fetch_array)編寫。這也是非常不安全的,你應該閱讀[SQL注入](http://stackoverflow.com/questions/60174/) – dsas

回答

0

您的HTML表單字段需要name屬性設置,因爲您尚未設置它們,所以它們將在$ _POST中進行數字索引,而不是按名稱索引,請使用print_r($ _ POST)來查看我的意思。

你需要更新你的HTML設置名稱=「」來命名=「國家」等

您還可能有一個問題,即在數據庫中的列名全部小寫(一般情況imo),並且您在第二個代碼示例中以不同的方式訪問它們。

嘗試使用print_r檢查$ row在while循環中包含的內容。你可能會發現你需要更改爲:

$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'];

+0

謝謝!我只需要更新名稱=「」。我有1錯誤,說未定義的索引:ID是** $ companyID = $ _POST ['ID']; **不知道有什麼問題。 – FocuZst

+0

你有'name =「companyID」',但在PHP代碼中以'$ _POST ['ID']'的身份訪問它。 PHP和HTML需要匹配。把它看作是對PHP說的HTML「把這個輸入元素的值放在input元素的name屬性中提到的請求索引中」 – dsas

+0

@FocuZst爲此提出一個新問題,它與這個問題無關。 – Jonast92