没有来自fetchAll()的结果;

时间:2014-03-03 08:22:13

标签: php mysql

我完全是PHP和MySQL世界的新手。我正在阅读Kevin Yank的书,在做他的一个例子时,我遇到了一个奇怪的结果。我确定我按照并正确输入了他书上写的代码,但我想知道为什么我没有得到相同的结果。我来回检查了代码并确定它是对的,或者我错过了什么。

我在这里张贴这个是因为我知道这里有很多对我这样的初学者非常有帮助和非常友好。任何输入都受到高度赞赏。期待发表评论,以便我可以继续学习,因为我认为我因为这个奇怪的错误而陷入困境。

请参阅以下代码供您参考。

的index.html

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Joke CMS</title>
</head>
<body>
    <h1>Joke Management System</h1>
    <ul>
        <li><a href="jokes/">Manage Jokes</a></li>
        <li><a href="authors/">Manage Authors</a></li>
        <li><a href="categories/">Manage Joke Categories</a></li>
    </ul>
</body>
</html>

的index.php

<?php

//Display author list
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try {
    $result = $pdo->query('SELECT id, name FROM author');
} catch (PDOException $e){
    $error = 'Error fetching authors from database! ' . $e->getMessage();
    include 'error.html.php';
    exit();
} 

if (isset($_POST['action']) and $_POST['action'] == 'Delete')
    {
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
        //Get jokes belonging to author
        try {
            $sql = 'SELECT id FROM joke WHERE authorid = :id';
            $s = $pdo->prepare($sql);
            $s->bindValue(':id', $_POST['id']);
            $s->execute();
        } catch (PDOException $e){
            $error = 'Error fetching authors with their jokes! ' . $e->getMessage();
            include 'error.html.php';
            exit();
        }

        $result = $s->fetchAll();

        //Delete joke category entries
        try {
            $sql = 'DELETE FROM jokecategory WHERE jokeid = :id';
            $s = $pdo->prepare($sql);

            //For each joke
            foreach ($result as $row)
                {
                    $jokeId = $row['id'];
                    $s->bindValue(':id', $jokeId);
                    $s->execute();
                }
        } catch (PDOException $e){
            $error = 'Error deleting joke category! ' . $e->getMessage();
            include 'error.html.php';
            exit();
        }

        //Delete jokes belonging to author
        try {
            $sql = 'DELETE FROM joke WHERE authorid = :id';
            $s = $pdo->prepare($sql);
            $s->bindValue(':id', $_POST['id']);
            $s->execute();
        } catch (PDOException $e){
            $error = 'Error deleting joke from a specific author! ' . $e->getMessage();
            include 'error.html.php';
            exit();
        }

        //Delete the author
        try {
            $sql = 'DELETE FROM author WHERE id = :id';
            $s = $pdo->prepare($sql);
            $s->bindValue(':id', $_POST['id']);
            $s->execute();
        } catch (PDOException $e){
            $error = 'Error deleting the author from database! ' . $e->getMessage();
            include 'error.html.php';
            exit();
        }

        header('Location: .');
        exit();
    }

foreach ($result as $row){
    $authors[] = array(
        'id' => $row['id'],
        'name' => $row['name']
        ); 
} 

include 'authors.html.php';
?>

authors.html.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helper.inc.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>Manage Authors</title>
</head>
<body>
    <h1>Manage Authors</h1>
    <p><a href="?add">Add new author</a></p>
    <ul>
        <?php foreach($authors as $author): ?>
            <li>
                <form action="" method="post">
                    <div>
                        <?php echo htmlout($author['name']); ?>
                        <input type="hidden" value="<?php echo htmlout($author['id']); ?>"/>
                        <input type="submit" name="action" value="Edit"/>
                        <input type="submit" name="action" value="Delete"/>
                    </div>
                </form>
            </li>
        <?php endforeach; ?>
    </ul>
    <p><a href="..">Return to JMS home</a></p>
</body>
</html>

所有输入都非常受欢迎。

2 个答案:

答案 0 :(得分:1)

  • 我不会问,$pdo对象是否已正确初始化,以及您是否在数据库中有实际记录。您使用Workbench等外部MySQL客户端测试查询。你也用它来优化。只有当您对查询感到满意时,才会将其放在脚本中,除非它如此简单,您可以正确地编写它。
  • `table_names``column_names`转换为MySQL合法,并使用;结束查询。它看起来更好。
  • 此外,在foreach初始化变量$authors = array();之前。那本书应该教你这个。
  • 学习使用var_dump()。将var_dump($result);放在$result = $pdo->query('...');之后。另请在var_dump($row);中使用foreach。它用于简单但有效的调试。打印变量值。
  • htmlout()确实存在?
  • var_dump()添加后,您实际看到了什么?

答案 1 :(得分:1)

在authors.html.php文件中,您没有为隐藏的输入类型指定名称

<input type="hidden" value="<?php echo htmlout($author['id']); ?>"/>

尝试更改为

<input type="hidden" name="id" value="<?php echo htmlout($author['id']); ?>"/>

您正在寻找一个不存在的POST变量

$s->bindValue(':id', $_POST['id']);