2016-11-27 182 views
0

我正試圖用下拉列表來構建一個簡單的表格來查詢我的數據庫(專爲課後計劃的圖書館設計)。這個想法是,下拉列表由書籍或學生的姓名填充,以便當您選擇姓名和提交時,數據庫將使用表中匹配的外鍵選擇詳細信息。注意:未定義的索引:變量

問題是,每當我打開的形式,我得到的表格上面的這些線路的幾個副本:

注意:未定義指數:ID在E:\ XAMPP \ htdocs中\ CMPS \圖書館\查詢\的index.php上線88

注意:未定義指數:名E:\ XAMPP \ htdocs中\上線CMPS \圖書館\查詢\的index.php 88

注意:未定義指數:ISBN在E: \ XAMPP \ htdocs \ CMPS \ Library \ query \ index.php on line 104

注意:未定義指數:標題E:上線104

同樣\ XAMPP \ htdocs中\ CMPS \圖書館\查詢\的index.php,每個下拉列表中的任一說

注意:未定義指數:名稱E:上線20

通知\ XAMPP \ htdocs中\ CMPS \圖書館\查詢\ searchform.html.php:U ndefined指數:標題在E:\ XAMPP \ htdocs中\ CMPS \圖書館\查詢\ searchform.html.php上線30

的index.php

<?php include_once 
'../includes/helpers.inc.php'; 

require_once '../access.inc.php'; 

if (!userIsLoggedIn()) 
{ 
    include '../login.html.php'; 
    exit(); 
} 

if (!userHasRole('Verified')) 
{ 
    $error = 'Only verified accounts may access this page.'; 
    include '../accessdenied.html.php'; 
    exit(); 
} 

if (isset($_GET['action']) and $_GET['action'] == 'search') 
{ 
    include '../includes/db.inc.php'; 

    $select = 'SELECT CONCAT_WS(\' \', student.First, student.Last) as Name, book.Title, checkout.Checked, checkout.Due'; 
    $from = ' FROM checkout, student, book'; 
    $where = ' WHERE student.ID = checkout.StudentID AND book.ISBN = checkout.BookISBN'; 

    $placeholders = array(); 

    if ($_GET['student'] != '') 
    { 
    $where .= " AND checkout.StudentID = :studentid"; 
    $placeholders[':studentid'] = $_GET['student']; 
    } 

    if ($_GET['book'] != '') 
    { 
    $where .= " AND checkout.BookISBN = :bookisbn"; 
    $placeholders[':bookisbn'] = $_GET['book']; 
    } 

    if (isset($_POST['ongoing']) && $_POST['ongoing'] == 'Yes') 
    { 
    $where .= " AND Ongoing = 1"; 
    } 

    try 
    { 
    $sql = $select . $from . $where; 
    $s = $pdo->prepare($sql); 
    $s->execute($placeholders); 
    } 
    catch (PDOException $e) 
    { 
    $error = 'Error fetching checkouts.'; 
    include 'error.html.php'; 
    exit(); 
    } 

    foreach ($s as $row) 
    { 
    $checkouts[] = array('Name' => $row['name'], 'book.Title' => $row['title'], 
         'Checked' => $row['checked'], 'Due' => $row['due']); 
    } 

    include 'query.html.php'; 
    exit(); 
} 

include '../includes/db.inc.php'; 

try 
{ 
    $result = $pdo->query('SELECT ID, CONCAT_WS(\' \', First, Last) as Name FROM student'); 
} 
catch (PDOException $e) 
{ 
    $error = 'Error fetching students from database!'; 
    include 'error.html.php'; 
    exit(); 
} 

foreach ($result as $row) 
{ 
    $students[] = array('ID' => $row['id'], 'Name' => $row['name']); #Line 88 
} 

try 
{ 
    $result = $pdo->query('SELECT ISBN, Title FROM book'); 
} 
catch (PDOException $e) 
{ 
    $error = 'Error fetching books from database!'; 
    include 'error.html.php'; 
    exit(); 
} 

foreach ($result as $row) 
{ 
    $books[] = array('ISBN' => $row['isbn'], 'Title' => $row['title']); #Line 104 
} 

include 'searchform.html.php'; 

searchform。 html.php

<?php include_once 
'../includes/helpers.inc.php'; ?> 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <!-- <link href="../style.css" rel="stylesheet" media="screen"> --> 
    <title>Query Checkouts</title> 
    </head> 
    <body> 
    <h1>Query</h1> 
    <form action="" method="get"> 
     <p>View checkouts satisfying the following criteria:</p> 
     <div> 
     <label for="student">By student:</label> 
     <select name="student" id="student"> 
      <option value="">Any student</option> 
      <?php foreach ($students as $student): ?> 
      <option value="<?php htmlout($student['id']); ?>"><?php 
       htmlout($student['name']); ?></option> <!-- Line 20 --> 
      <?php endforeach; ?> 
     </select> 
     </div> 
     <div> 
     <label for="book">By book:</label> 
     <select name="book" id="book"> 
      <option value="">Any book</option> 
      <?php foreach ($books as $book): ?> 
      <option value="<?php htmlout($book['isbn']); ?>"><?php 
       htmlout($book['title']); ?></option> <!-- Line 30 --> 
      <?php endforeach; ?> 
     </select> 
     </div> 
     <div> 
     <label for="ongoing">Ongoing only?:</label> 
     <input type="checkbox" name="ongoing" value="Yes" /> 
     </div> 
     <div> 
     <input type="hidden" name="action" value="search"> 
     <input type="submit" value="Search"> 
     </div> 
    </form> 
    <p><a href="..">Return to PHL home</a></p> 
    <?php include '../logout.inc.html.php'; ?> 
    </body> 
</html> 

我試圖檢查碼,b ut phpcodechecker.com表示沒有錯誤,我很難用Chrome Logger進行調試。

希望我能理解我做錯了什麼,所以我不會在以後的發展中犯錯!

+0

你也經常混合使用html和php,它不可讀,不要這樣做代碼'<! - 30行 - > <?php endforeach; '' –

+1

[PHP:「Notice:Undefined variable」和「Notice:Undefined index」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined -index) – Dezza

回答

3

在訪問循環中的$row時,需要確保使用區分大小寫的索引。

變化:

$students[] = array('ID' => $row['id'], 'Name' => $row['name']); 

要:

$students[] = array('ID' => $row['ID'], 'Name' => $row['Name']); 

此外,在searchform.html.php你需要爲你定義它們,即訪問陣列。使用IDName來訪問這些值。或者更改上述更改中的這些索引以匹配預期的小寫版本。

例子:

$students[] = array('id' => $row['ID'], 'name' => $row['Name']); 

只要你知道,這可能會影響其他迴路的結果太。只要對這些應用相同的更改,您應該也可以(例如$checkouts似乎也受到影響)。

+0

現在我覺得啞巴。這完全奏效!謝謝! –