带有多个标签的PHP MySQL搜索

问题描述 投票:0回答:1

我正在创建一个网站,用户可以在其中上传带有很多标签的图像,并且还具有搜索功能来显示带有这些标签的帖子,但是我在显示带有标签的帖子时遇到了麻烦:这对我来说太令人困惑了并为图片添加了两个不同的MySQL表,然后是标签。

我的MySQL图片表:

id
img_url
img_id
title
description
username
status
positive        
negative        
date_time

我的MySQL标签表:

id
img_id
tag_name

我的PHP代码:

/* put the tags in the url */
if(isset($_POST['s_submit'])){
    $tags = strip_tags(stripcslashes(htmlspecialchars($_POST['search_box'])));
    header("location:home.php?search={$tags}");
}
/* search image tags */
if(isset($_GET['search'])){
    $tags = strip_tags(stripslashes(htmlspecialchars($_GET['search'])));
    if(!empty($tags)){
        $tags_o = explode("%27", $tags);
        $tags_c = count($tags_o);
        for ($i=0; $i < $tags_c ; $i++){
            $con = new mysqli($db_host,$db_user,$db_pass,$db_name);
            $tags_query = $con->query("SELECT * FROM tags WHERE tag_name LIKE '$tags_o[$i]'");
            $tags_array = mysqli_fetch_array($tags_query);
            $tags_i = $tags_array['img_id'];
            $post_query = $con->query("SELECT * FROM posts WHERE img_id='$tags_i'");
            while($post_array = mysqli_fetch_array($post_query)){
                echo '
                <div class="post">
                    <a href="#">
                        <div class="post-img">
                            '.$post_array['img_url'].'
                        </div>
                    </a>
                    <div class="post-btm">
                        <p>From: <a href="#">Admin</a></p>
                    </div>
                </div>
                ';
            }
        }
    } else {
        echo '<div class="search-error"><p>Nothing to show here, try to search something!</p></div>';
    }
}
php html mysql
1个回答
0
投票

尝试使用WHERE columnName IN (),如果写得正确,则可以通过一个查询来完成

© www.soinside.com 2019 - 2024. All rights reserved.