我有未定义的变量通知和无效的参数foreach警告

时间:2014-12-10 00:33:11

标签: php mysql foreach

我是PHP和MySQL的新手。我正在做一个项目,我不确定我做错了什么。如果有人能够如此友好地看我的代码并给我指导会很棒。我在这里搜索过,找不到类似于我可以使用的问题。

这些是我的错误:

  

注意:未定义的变量:第10行的C:\ xampp \ htdocs \ project3 \ autotrips2014 \ autotrips2014.php中的类别

     

警告:在第10行的C:\ xampp \ htdocs \ project3 \ autotrips2014 \ autotrips2014.php中为foreach()提供的参数无效

     

注意:未定义的变量:第32行的C:\ xampp \ htdocs \ project3 \ autotrips2014 \ autotrips2014.php中的autotrips2014

     

警告:在第32行的C:\ xampp \ htdocs \ project3 \ autotrips2014 \ autotrips2014.php中为foreach()提供的参数无效

这是我的代码:

<?php
require('../model/database.php');
require 'autotrips2014.php';


// Get category
if (isset($_GET ['category'])){
        $category=$_GET ['category'];
    }
    else {
        $category='business';
    }

// Get categories
$query = 'SELECT category FROM autotrips2014';
$categories = $db->query($query);

// Get products for selected category
$query = "SELECT * FROM autotrips2014
        WHERE category = $category
        ORDER BY tripdate";
$autotrips2014 = $db->query($query);

?>

HTML:

<?php include '../view/header.php';?>
<div id="main">

    <h1>Auto Trips 2014</h1>

    <div id="sidebar">
        <!-- display menu options -->
        <h2>Menu</h2>
        <ul class="nav">
        <?php foreach ($categories as $cat) : ?>
                <li>
                <a href="?category=<?php echo $cat['category']; ?>">
                    <?php echo $cat['category']; ?>
                </a>
                </li>
            <?php endforeach; ?>
            </ul>
    </div>

    <div id="content">
        <!-- display the miles -->

        <table>
            <tr>
                <th>Trip ID</th>
                <th>Trip Date</th>
                <th>City</th>
                <th>Begin Odometer</th>
                <th>End Odometer</th>
                <th>Category</th>
            </tr>
            <?php foreach ($autotrips2014 as $auto) : ?>
            <tr>
                <td><?php echo $auto['tripid']; ?></td>
                <td><?php echo $auto['tripdate']; ?></td>
                <td><?php echo $auto['city']; ?></td>
                <td><?php echo $auto['beginodometer']; ?></td>
                <td><?php echo $auto['endotometer']; ?></td>
                <td><?php echo $auto['category']; ?></td>
                <td><form action="." method="post">
                    <input type="hidden" name="action"
                           value="delete_trip" />
                    <input type="hidden" name="trip_id"
                           value="<?php echo $auto['tripid']; ?>" />
                    <input type="submit" value="Delete" />
                </form></td>
            </tr>
            <?php endforeach; ?>
        </table>
        <p><a href="trip_add.php">Add Trip</a></p>
    </div>

</div>
<?php include '../view/footer.php'; ?>

提前谢谢。

0 个答案:

没有答案