Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP搜索页面返回错误,但搜索功能正常_Php_Post_Search_Foreach_Isset - Fatal编程技术网

PHP搜索页面返回错误,但搜索功能正常

PHP搜索页面返回错误,但搜索功能正常,php,post,search,foreach,isset,Php,Post,Search,Foreach,Isset,我已经创建了一个搜索页面,可以搜索数据库中posts表中的列。当我进入搜索页面时,我得到一个错误注意:未定义索引:在/app/public/search.php中搜索,但是如果我搜索数据库中存在的单词,搜索将起作用。我预计会出现错误,因为在提交表单之前,$search不会被识别。这是密码 <form class="form-inline" action="search.php" method="post"> <

我已经创建了一个搜索页面,可以搜索数据库中posts表中的列。当我进入搜索页面时,我得到一个错误
注意:未定义索引:在/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>

我在努力理解我做错了什么。有人能给我一个指向正确方向的指针吗?

错误来自foreach循环。将foreach循环的isset和空检查添加到

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

图像);?>“alt=“post image”>
创建于
20条评论


如果未设置
$\u POST['search']
,则不运行查询,但仍会运行
foreach($posts…
$posts
尚未设置。您能否回显您的$sql并粘贴到此处。我相信,您的语句并没有按照您预期的那样准备好。同意@abracadaver您可以在
闪存之后添加一个
退出;
(…你的脚本是开放的。即使你应该考虑使用<代码> MySqLyI<<代码>或<代码> PDO< /COD> API,而不是级联值。谢谢你。我没有想到将ISStand语句添加到代码的那一部分,并没有意识到你可以这样做——我学到了一些新的东西,那真是太棒了!谢谢你的帮助。
<?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; } ?>