我希望能够在我的php代码中使用select中的值,但每当我收到一条说明未定义索引类别的通知时,我做错了什么?我搜索过与此相似的内容,他们都告诉我做我已经做过的事,有人可以帮忙吗?
<select name="categorie" value="all">
<option value="all">All</option>
<option value="action">Action</option>
<option value="romance">Romance</option>
<option value="comedy">Comedy</option>
<option value="drama">Drama</option>
<option value="horror">Horror</option>
<option value="fantasy">Fantasy</option>
<option value="scifi">Sci-fi</option>
</select>
<br>
<form method="post" action="">
<input type="submit" value="Search" name="search">
</form>
<?php
$db = new PDO('mysql:host=localhost;dbname=id1552202_accounts', 'id1552202_thecouch', 'Fargo123');
if(isset($_POST['search'])){
$categorie = $_POST['categorie'];
if( $categorie === "all"){
$all = $db->prepare("SELECT name, path FROM Reviews ORDER BY date");
$all->execute();
while($result = $all->fetch(PDO::FETCH_ASSOC)){
echo $result['name'];
echo '<br>';
echo $result['path'];
}
} else
$selectreview = $db->prepare("SELECT name, path FROM Reviews WHERE genre = :categorie ORDER BY date");
$selectreview->bindParam("categorie", $categorie);
$selectreview->execute();
while($result = $selectreview->fetch(PDO::FETCH_ASSOC)){
echo $result['name'];
echo '<br>';
echo '<br>';
echo $result['path'];
}
}
答案 0 :(得分:2)
您的类别未定义,为什么您遇到该错误,您必须定义该类别。在第一个条件内过滤得很好,但需要在else语句中进行改进。另一点是选择外部形式:这样它就永远不会得到选择的值,放在像@addie说的那样的形式里。
$selectreview = $db->prepare("SELECT name, path FROM Reviews WHERE genre = :categorie ORDER BY date");
$selectreview->bindParam("categorie", $categorie);
$selectreview->execute();
while($result = $selectreview->fetch(PDO::FETCH_ASSOC)){
echo $result['name'];
echo '<br>';
echo '<br>';
echo $result['path'];
}
我的建议是,在其他方面,在没有类别绑定的情况下进行评论,只是在用户选择一个过滤之前填充你的前端!
} else
$selectreview = $db->prepare("SELECT name, path FROM Reviews ORDER BY date, categorie");
$selectreview->execute();
while($result = $selectreview->fetch(PDO::FETCH_ASSOC)){
echo $result['name'];
echo '<br>';
echo '<br>';
echo $result['path'];
}
答案 1 :(得分:1)
将<form>
置于html之上,为我工作
<form method="post" action="">
<select name="categorie">
<option value="all">All</option>
<option value="action">Action</option>
<option value="romance">Romance</option>
<option value="comedy">Comedy</option>
<option value="drama">Drama</option>
<option value="horror">Horror</option>
<option value="fantasy">Fantasy</option>
<option value="scifi">Sci-fi</option>
</select>
<input type="submit" value="Search" name="search">
</form>