2013-06-20 36 views
-1

我寫的作品,但我仍然不斷收到代碼:注意:未定義指數:ID在

注意:未定義指數:ID在C:\ XAMPP \ htdocs中\組合\ periode4 \園藝\陣列。 php on line 35.

有沒有人知道我必須做什麼來帶走通知?

<?php 
function connect() 
    { 
    // Make a MySQL Connection 
    $link = mysqli_connect("localhost", "root", "", "school") or die(mysqli_error()); 
    return $link; 
    } 


if($_GET) { 
    $id = $_GET["id"]; 
} 

// select query 
$query = "SELECT * FROM graden" ; 


$result = mysqli_query(connect(),$query); 


// tabel maken 
$table = "<table border='1'>"; 

$table .= "<tr> 
      <th> ID </th> 
      <th>Graden Celcius</th> 
      <th>Aanduiding</th> 
      <th>Image</th> 
     </tr>"; 


// keeps getting the next row until there are no more to get 
    while($row = mysqli_fetch_assoc($result)) { 

    $table .="<tr ".($_GET['id'] == $row['id']?" style='background-color:yellow'":"").">"; 

    // Print out the contents of each row into a table 
    foreach ($row as $key => $value) 
     { 
      if ($key == "Image") 
      { 
       $table .="<td><img src='$value' /></td>"; 
      } 
       elseif ($key == "id") 
       { 
        $table .="<td><a href='array.php?id=$value'>$value</a></td>"; 
       } 
        else 
        { 
         $table .="<td>$value</td>";   
        } 

     } 
     $table .= "</tr>"; 
     } 
     $table .="</table>"; 

     echo $table; 
    // optie lijst 

//maken select query 

$query = "SELECT id, temp FROM graden "; 


//uitvoeren select query 
$result = mysqli_query(connect(), $query) ; 


    //tonen alle gegevens 
    $select = " 
     <form action=\"array.php\" method=\"get\">\n 
      <select name=\"id\">\n 
       <option value='id'>---all---</option>\n 
    "; 
    while($row = mysqli_fetch_assoc($result)) 
     { 
      $select .= "<option value='".$row['id']."'>".$row["temp"]."</option>\n"; 
     } 
      $select .= "</select>\n"; 
      $select .= "<input type=\"submit\">\n"; 
      $select .= "</form>\n"; 

    echo $select; 
+0

你能計算行數並標記第35個?謝謝。 –

+0

看來你應該在while循環中使用'$ id'(而不是'$ _GET ['id']')。 – landons

+0

您可以從閱讀PHP文檔的這一部分開始:http://php.net/manual/en/language.types.array.php它包含對此錯誤的解釋。 – Boldewyn

回答

1

如果有對URL沒有id參數,這將失敗課程:

$table .="<tr ". ($_GET['id'] == $row['id']?" style='background-color:yellow'":"").">"; 

嘗試

$table .="<tr ". (isset($_GET['id']) && $_GET['id'] == $row['id']?" style='background-color:yellow'":"").">"; 

但是這可能會打破邏輯了一下,你需要檢查..

+0

偉大的工作。非常感謝你 –