0

无法让我的脚本正确运行,我有 2 个未定义的索引错误和一个无效的参数提供的错误,在我的一生中我无法弄清楚我为什么会得到。2 个未定义的索引错误来自这些行。

if(!is_null($_GET['order']) && $_GET['order'] != 'courseTitle')

if (!is_null($_GET['page']))

我的无效参数错误是这个

警告:为 foreach() 提供的参数无效

由此产生

<?php foreach ($books as $book) : ?>

我在两个类之间的完整代码是这样的..关于我做错了什么的任何想法?把我的头发扯掉了。

index.php 
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Catalog</title>
    </head>
    <body bgcolor="white">
        <?php
            /////////////////////////////////////////////////
            //connect to db
            /////////////////////////////////////////////////
            $dsn = 'mysql:host=localhost;dbname=book_catalog';
            $username = "php";
            $password = "php";
            $db = new PDO($dsn, $username, $password);

            //get data
            if(!is_null($_GET['order']) && $_GET['order'] != 'courseTitle')
            {
                $thesort = $_GET['order'];
                $query = "Select * FROM book
                INNER JOIN course
                ON book.course = course.courseID
                ORDER BY ".$_GET['order'];
            }
            else
            {
                $thesort = "courseTitle";
                $query = "Select * FROM book
                INNER JOIN course
                ON book.course = course.courseID
                ORDER BY $thesort";
            }

            //if page is null go to first page otherwise query for correct page
            if (!is_null($_GET['page']))
            {
                $query = $query." LIMIT ".($_GET['page']*8-8).", 8";
            }
            else
            {
                $query = $query." LIMIT 0, 8";
            }

            //query result
            $books = $db->query($query);

            //get number of overall rows
            $query2 = $db->query("SELECT * FROM book");
            $count = $db->query("SELECT Count(*) As 'totalRecords' FROM book");
            $count = $count->fetch();
            $count = $count['totalRecords'];
        ?>

        <table border =" 1">
            <tr>
                <th bgcolor="#6495ed"><a href="?order=course">Course #</th>
                <th bgcolor="#6495ed"><a href="?order=courseTitle">Course Title</th>
                <th bgcolor="#6495ed"><a href="?order=bookTitle">Book Title</th>
                <th bgcolor="#6495ed"></th>
                <th bgcolor="#6495ed"><a href="?order=price">Price</th>
            </tr>
            <?php foreach ($books as $book) : ?>
            <tr>
                <td><a href="rsc/catalog.pdf"><?php echo $book['course']; ?></a></td>
                <td><?php echo $book['courseTitle']; ?></td>
                <td><?php echo $book['bookTitle']; ?></td>
                <td><?php
                $bookcourse = $book['course'];
                $isbn = $book['isbn13'];
                $booklink = "<a href=\"course.php?course=$bookcourse&isbn=$isbn\">";
                echo $booklink ;?><img src='images/<?php echo $book['isbn13'].'.jpg'; ?>'></a></td>
                <td><?php echo $book['price']; ?></td>
            </tr>
            <?php endforeach; ?>
            </tr>
        </table>
         <?php
         //paging function... not sure if it works correctly?
            for ($j=1; $j <= ceil($count/8); $j++)
            { ?>

            <a href=<?php echo "?page=".$j."&order=".$thesort; ?>><?php echo $j; ?></a>
            <?php
            }?>
    </body>
</html>


**course.php**

<?php

    //get data from index.php
    $course = $_GET['course'];
    $isbn = $_GET['isbn'];    

        //connect to db
        $dsn = 'mysql:host=localhost;dbname=book_catalog';
        $username = "php";
        $password = "php";
        $db = new PDO($dsn, $username, $password);

        //get data
        $query = "Select * FROM book, course, author, publisher
    WHERE book.isbn13 = $isbn AND book.course = '$course' AND book.course = course.courseID AND book.bookID = author.bookID AND book.publisher = publisher.publisherID
            ORDER BY book.bookID";

        //query results        
        $books = $db->query($query);

        //error troubleshooting
          if (!$books) {
            echo "Could not successfully run query ($query) from DB: " . mysql_error();
            exit;
          }


        //count the number of rows in the result
        $results = $books->fetchAll();
        $rowCount = count($book);

        //get data from results
        foreach($results as $book){
            $bookID = $book['bookID'];
            $bookTitle = $book['bookTitle'];
            $isbn = $book['isbn13'];
            $price = $book['price']; 
            $desc = $book['description'];
            $publisher = $book['publisher'];
            $courseTitle = $book['courseTitle'];
            $courseID = $book['courseID'];
            $credits = $book['credit'];
            $edition = $book['edition'];
            $publishDate = $book['publishDate'];
            $length = $book['length'];
            $firstName = $book['firstName'];
            $lastName = $book['lastName'];

        }

        if($numrows > 1)
            {
                foreach ($books as $book)
                {
                    $authorArray[] = $book['firstName'] + ' ' + $book['lastName'];
                }
            }





?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>CIS Department Book Catalog</title>
    </head>
    <body bgcolor=white">

        <table border="0">
            <tr>
                <td>
                    <img src='images/<?php echo $isbn.'.jpg'; ?>'>
                </td>
                <td>
         <?php
         echo "For Course: $courseID  $courseTitle ($credits)";
         echo "</br>";    
         echo "Book Title: $bookTitle";
         echo "</br>";    
         echo "Price: $price";
         echo "</br>";    
         echo "Author";
         if ($numResults > 1)
         {
             echo "s:";
             for ($i = 0; $i < $numResults; $i++)
             {
                 if ($i!=0)
                 echo ", $authorArray[i]";
                 else
                     echo $authorArrat[i];
             }
         }
         else
             echo ": $firstName, $lastName";

         echo "</br>";    
         echo "Publisher: $publisher";
         echo "</br>";    
         echo "Edition: $edition ($publishDate)";
         echo "</br>";    
         echo "Length: $length pages";
         echo "</br>";    
         echo "ISBN-13: $isbn";
         ?>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <?php echo "Description: $desc"; ?>
                </td>
            </tr>
        </table>

    </body>

</html>
4

1 回答 1

1
  1. 您应该使用issetnotis_null来防止它警告未定义的变量。

  2. $books从未定义它被定义,只是错误地......foreach需要它是一个数组。无论如何你真的不需要它,用while循环将每一行提取到数组中。(见下面我的例子)。您还在$count查询中多次重新定义。

就像@Brad 说的那样。使用准备好的语句和占位符。您的数据库最终会被您当前的代码入侵。

编辑

回答你的问题。query()返回一个语句句柄。(我已将其定义为$sth)。fetch()返回一个结果,您需要传递其中一个 fetch 模式常量(或默认情况下早先定义它$db->setFetchMode()

要获得您需要的书籍

$books = array();
$sth = $db->query($query);
while( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
    $books[] = $row; // appends each row to the array
}

以下是您的代码应该如何计数。

// you're not using the $query2 you defined ... just remove it
$sth = $db->query("SELECT Count(*) As 'totalRecords' FROM book");
$result = $sth->fetch(PDO::FETCH_ASSOC);
$count = $result['totalRecords'];

看看: http : //wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers 看起来是一本很好的指南,可以让您深入了解如何使用 PDO。特别注意错误处理和准备好的语句!

于 2013-05-01T03:12:52.040 回答