dtn43447 2016-11-27 07:22
浏览 80
已采纳

注意:未定义的索引:变量

I'm trying to build a simple form with drop-down lists to query my database (designed for a library for an afterschool program). The idea is that the drop-down lists are populated by the names of books or students, so that when you select a name and Submit, the database selects details with a matching foreign key from the table.

Problem is, whenever I open up the form, I get several copies of these lines above the form:

Notice: Undefined index: id in E:\XAMPP\htdocs\CMPS\Library\query\index.php on line 88

Notice: Undefined index: name in E:\XAMPP\htdocs\CMPS\Library\query\index.php on line 88

Notice: Undefined index: isbn in E:\XAMPP\htdocs\CMPS\Library\query\index.php on line 104

Notice: Undefined index: title in E:\XAMPP\htdocs\CMPS\Library\query\index.php on line 104

Likewise, each of the drop-down lists says either

Notice: Undefined index: name in E:\XAMPP\htdocs\CMPS\Library\query\searchform.html.php on line 20

or

Notice: Undefined index: title in E:\XAMPP\htdocs\CMPS\Library\query\searchform.html.php on line 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>

I tried checking the code, but phpcodechecker.com says there are zero errors, and I'm having a hard time using Chrome Logger to debug.

Hoping I can understand what I'm doing wrong so I won't make the mistake later in development!

  • 写回答

1条回答 默认 最新

  • drpkcwwav20524605 2016-11-27 07:26
    关注

    You need to make sure to use case-sensitive indices when accessing the $row in your loop.

    Change:

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

    To:

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

    Additionally, in searchform.html.php you need to access your arrays as you have defined them, ie. use ID and Name to access those values. Or change those indices in the above alteration to match the expected lowercase versions.

    Example:

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

    Just so you know, this may be affecting the other result loops too. Just apply the same changes to those as well and you should be fine (eg. $checkouts appears to also be affected).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛