2015-01-02 88 views
0

我得到在我的網頁下面的5個錯誤:爲什麼我會得到「未定義的索引」?

Notice: Undefined index: town in C:\xampp\htdocs\dispatch1.php on line 51 

Notice: Undefined index: location in C:\xampp\htdocs\dispatch1.php on line 52 

Notice: Undefined index: incident_type in C:\xampp\htdocs\dispatch1.php on line 53 

Notice: Undefined index: time_date in C:\xampp\htdocs\dispatch1.php on line 54 

Notice: Undefined index: admin in C:\xampp\htdocs\dispatch1.php on line 55 

這裏是我是從數據庫中獲取數據(它確實表明從數據庫精細的數據),我的輸出表的代碼:

<?php include("manage_post.php"); ?> 
<!DOCTYPE html> 
<html lang='en'> 
<head> 
<meta http-equiv="refresh" content="2" > 
</head> 

<body> 
<div align="center" class="CSSTableGenerator" height="100%"> 
    <form action="dispatch1.php" method="get" id="dispatch"> 
<table width="968" height="248" border="1" align="center" cellpadding="10" cellspacing="0" rules="rows" id="incidents" style="color:#333333;border-collapse:collapse;text-align:left;"> 
    <tr style="color:White;background-color:#5D7B9D;font-weight:bold;font-style:italic;"> 
    <th scope="col">TOWN</th> 
    <th scope="col">LOCATION</th> 
    <th scope="col">INCIDENT TYPE</th> 
    <th scope="col">TIME/DATE</th> 
    <th scope="col">ADMIN</th> 
    </tr> 
    <tr style="color:#333333;background-color:#F7F6F3;font-weight:bold;"> 
    <?php 
    $town = $_POST['town']; 
    $location = $_POST['location']; 
    $incident_type= $_POST['incident_type']; 
    $time_date= $_POST['time_date']; 
    $admin = $_POST['admin']; 

    $db = mysqli_connect('localhost','root','') or die("Database error"); 
    mysqli_select_db($db, 'mydatabase'); 
    $result= mysqli_query($db, "select * from cad"); 
    while($row = mysqli_fetch_array($result)) 
    { 
     echo "<td>" .$row['town'] ."</td>"; 
     echo "<td>" .$row['location'] ."</td>"; 
     echo "<td>" .$row['incident_type'] ."</td>"; 
     echo "<td>" .$row['time_date'] ."</td>"; 
     echo "<td>" .$row['admin'] ."</td>"; 
    } 
    ?> 
    </tr> 
    </table> 
    </form> 
    </body> 
    </html> 

然後,這是我的「manage_post.php」頁面,並將數據從表單的數據庫:

<?php 

if($_POST) 
{ 

    $con = mysql_connect("localhost","root",""); 

    if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db("mydatabase", $con); 


    $town = $_POST['town']; 
    $location = $_POST['location']; 
    $incident_type = $_POST['incident_type']; 
    $time_date = $_POST['time_date']; 
    $admin = $_POST['admin']; 


    $town = mysql_real_escape_string($town); 
    $location = mysql_real_escape_string($location); 
    $incident_type = mysql_real_escape_string($incident_type); 
    $time_date = mysql_real_escape_string($time_date); 
    $admin = mysql_real_escape_string($admin); 


    $query = " 
    INSERT INTO `mydatabase`.`cad` (`town`, `location`, `incident_type`, `time_date`, `admin`) VALUES ('$town', '$location', '$incident_type', '$time_date', '$admin');"; 

    mysql_query($query); 

    mysql_close($con); 
} 

?> 

我試圖通過它和f找出它有什麼問題,但找不到它。如果您需要表單代碼,請告訴我。我的PHP代碼的另一個奇怪的問題是,表格不是垂直的,而是水平的,並且一直在屏幕上。 Here is a picture of it.然後我必須水平滾動才能查看所有內容。

+0

您是否將變量聲明在文檔的頂部?例如¤var=「」; –

+0

實際上在'cad'表中有'town'列嗎? – 0b10011

+1

您的表很長,因爲您的不在循環中。在while循環中移動和。 –

回答

2

在您展示的第一個文件,你想訪問POST值:

$town = $_POST['town']; 
$location = $_POST['location']; 
$incident_type= $_POST['incident_type']; 
$time_date= $_POST['time_date']; 
$admin = $_POST['admin']; 

但目前尚無公佈。您仍然顯示將發佈的表單。如何顯示錶單依賴於已提交的相同表單?

爲了有條件地檢查數值已經過帳,你可以做你在你的第二個文件做同樣的事情:

if($_POST) 
{ 
    // use the POST values 
} 

或者,您可以有條件地檢查每個單獨的值:

if(isset($_POST['town'])) 
{ 
    // use the town value 
} 

if(isset($_POST['location'])) 
{ 
    // use the location value 
} 

// etc. 

至於你想要的時候有沒有值顯示什麼,這取決於你。

+0

很好的解釋!非常感謝! – sam44

0

檢查變量

if(isset($_POST['town'])) { 
$town = $_POST['town']; 
} 
if(isset($_POST['location'])) { 
$location = $_POST['location']; 
} 
0

$ _POST$ _GET是PHP的兩個特殊功能,用於從用戶填寫的表單中獲取變量。雖然使用這些功能,用戶可能會遇到錯誤

  • 說明:未定義索引。

這個錯誤可以用PHP isset()的幫助來避免。將會通知此錯誤,但這取決於服務器的配置。注意:未定義索引是一個小錯誤,因此默認情況下不會通知。藉助error_reporting函數,可以更改報告的錯誤類型。

0

它在代碼

$town = $_POST['town']; 
$location = $_POST['location']; 
$incident_type= $_POST['incident_type']; 
$time_date= $_POST['time_date']; 
$admin = $_POST['admin']; 

的這部分問題沒有指數「鎮」,「位置」,......在數組$ _POST。您可以使用isset輕鬆修復聲明,以便檢查索引是否存在。如果是這樣,將值賦給變量$ town ...

if(isset($_POST['town'])) { ... } 
0

可以避開所有if ... isset和只轉儲什麼是POST命名一樣貼指標變量。你真的只需要/ isset來查看特定發佈變量中的內容,但如果它不重要,這是避免遇到錯誤的最簡單方法。

foreach($_POST as $k=>$v){ 
${$k}=mysql_real_escape_string($v); 
} 
相關問題