我在HTML中有这个表单
<form action="" method="post">
<div class="tags">
<label class="tag" for="TAG1"><input type="checkbox" id="TAG1" value="TAG1" name="tags[]">TAG1</label>
<label class="tag" for="TAG2"><input type="checkbox" id="TAG2" value="TAG2" name="tags[]">TAG2</label>
<label class="tag" for="TAG3"><input type="checkbox" id="TAG3" value="TAG3" name="tags[]">TAG3</label>
<label class="tag" for="TAG4"><input type="checkbox" id="TAG4" value="TAG4" name="tags[]">TAG4</label>
<label class="tag" for="TAG5"><input type="checkbox" id="TAG5" value="TAG5" name="tags[]">TAG5</label>
</div>
<button type="submit" name="submit">submit</button>
</form>
然后这是我的PHP简介
if(isset($_POST['submit'])){
$tag = $_POST['tags']
$query = "
SELECT a.title FROM posts a
INNER JOIN tags b ON a.id = b.pid
WHERE b.tag IN (:tag)
";
$stmt = $pdo->prepare($query);
$stmt->execute(['tag' => $tag]);
}
如何在我的代码查询中准确搜索多个选定的标签?我一直在
Notice: Array to string conversion
答案 0 :(得分:1)
MySql IN
接受由comma(,)
分隔的字符串,并且您正在将数组传递给其中,因此您将收到警告消息,只是将您的标记数组内爆以使其成为字符串
if(isset($_POST['submit'])){
$tag = implode(",",$_POST['tags']);
$query = "
SELECT a.title FROM posts a
INNER JOIN tags b ON a.id = b.pid
WHERE b.tag IN (:tag)
";
$stmt = $pdo->prepare($query);
$stmt->execute(['tag' => $tag]);
}