John Cowan John Cowan - 6 days ago 6
Apache Configuration Question

Notice: Undefined index: variable

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!

Answer

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).

Comments