Search code examples
phppostsearchforeachisset

PHP search page returning error but the search function works


I have created a search page that will search columns in the posts table in the database. When I go to the search page I get the error Notice: Undefined index: search in /app/public/search.php, but search will work if I search for a word that exists in the database. I expected the error since $search won't be recognized until the form is submitted. Here's the code.

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

I tried using if(isset($_POST['search'])) which stops the error, but then I get the message Undefined variable: posts in /app/public/search.php. Here's the code:

<?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>

I'm trying to understand what I am doing wrong. Can anyone give me a pointer in the right direction please?


Solution

  • That error is came from 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; } ?>