PHP 搜索页面返回错误但搜索功能有效

PHP search page returning error but the search function works

我创建了一个搜索页面,用于搜索数据库中帖子 table 中的列。当我转到搜索页面时,出现错误 Notice: Undefined index: search in /app/public/search.php,但如果我搜索数据库中存在的词,搜索将起作用。我预计会出现错误,因为 $search 在提交表单之前不会被识别。这是代码。

<form class="form-inline" action="search.php" method="post">
    <input class="form-control" type="text" name="search" placeholder="Search"><br>
    <button class="btn btn-primary" type ="submit">Submit</button>
</form>
<?php
    $search = $_POST['search'];

    $sql = "SELECT * FROM posts WHERE post LIKE '%$search%' OR title LIKE '%$search%' OR datetime LIKE '%$search%'";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $posts = $stmt->fetchAll();
    $postCount = $stmt->rowCount();

    if ($postCount < 1) {
        flash('no_posts_found', '<h3>No Posts Found</h3>', 'lead');
    }
?>

<?php foreach($posts as $post) : ?>
    <div class="card my-4">
        <img class="img-fluid card-img-top" src="uploads/<?php echo htmlentities($post->image);  ?>" alt="post image">
        <div class="card-body">
            <h2 class="card-title"><a href="post.php?id=<?php echo $post->id; ?>"><?php echo htmlentities($post->title); ?></a></h2>
            <small>Created on <?php echo htmlentities($post->datetime); ?> by <?php echo htmlentities($post->author); ?></small>
            <span class="float-right badge badge-dark">20 Comments</span>
            <hr>
            <p><?php echo htmlentities(mb_strimwidth($post->post, 0, 150, '...')); ?></p>
            <a class="btn btn-primary float-right" href="post.php?id=<?php echo htmlentities($post->id); ?>">Read More<i class="fas fa-angle-double-right ml-1"></i></a>
        </div>
    </div>
<?php endforeach; ?>
</div>

我尝试使用 if(isset($_POST['search'])) 来停止错误,但随后我收到消息 Undefined variable: posts in /app/public/search.php。这是代码:

<?php if(isset($_POST['search'])) {
    $search = $_POST['search'];

    $sql = "SELECT * FROM posts WHERE post LIKE '%$search%' OR title LIKE '%$search%' OR datetime LIKE '%$search%'";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $posts = $stmt->fetchAll();
    $postCount = $stmt->rowCount();

    if ($postCount < 1) {
        flash('no_posts_found', '<h3>No Posts Found</h3>', 'lead');
    }
}
?>

<?php foreach($posts as $post) : ?>
    <div class="card my-4">
        <img class="img-fluid card-img-top" src="uploads/<?php echo htmlentities($post->image);  ?>" alt="post image">
        <div class="card-body">
            <h2 class="card-title"><a href="post.php?id=<?php echo $post->id; ?>"><?php echo htmlentities($post->title); ?></a></h2>
            <small>Created on <?php echo htmlentities($post->datetime); ?> by <?php echo htmlentities($post->author); ?></small>
            <span class="float-right badge badge-dark">20 Comments</span>
            <hr>
            <p><?php echo htmlentities(mb_strimwidth($post->post, 0, 150, '...')); ?></p>
            <a class="btn btn-primary float-right" href="post.php?id=<?php echo htmlentities($post->id); ?>">Read More<i class="fas fa-angle-double-right ml-1"></i></a>
        </div>
    </div>
<?php endforeach; ?>
</div>

我正在努力了解我做错了什么。谁能给我一个正确方向的指示?

该错误来自 foreach loop.Add isset and empty check for foreach loop to.

<?php if( isset($posts) && !empty($posts) ) { foreach($posts as $post) : ?>
    <div class="card my-4">
        <img class="img-fluid card-img-top" src="uploads/<?php echo htmlentities($post->image);  ?>" alt="post image">
        <div class="card-body">
            <h2 class="card-title"><a href="post.php?id=<?php echo $post->id; ?>"><?php echo htmlentities($post->title); ?></a></h2>
            <small>Created on <?php echo htmlentities($post->datetime); ?> by <?php echo htmlentities($post->author); ?></small>
            <span class="float-right badge badge-dark">20 Comments</span>
            <hr>
            <p><?php echo htmlentities(mb_strimwidth($post->post, 0, 150, '...')); ?></p>
            <a class="btn btn-primary float-right" href="post.php?id=<?php echo htmlentities($post->id); ?>">Read More<i class="fas fa-angle-double-right ml-1"></i></a>
        </div>
    </div>
<?php endforeach; } ?>