gpt4 book ai didi

javascript - JQuery Mobile Mysql数据库过滤系统

转载 作者:行者123 更新时间:2023-11-29 19:32:06 25 4
gpt4 key购买 nike

我希望为我的数据库记录建立一个过滤系统。我可以加载记录,但我希望使用 JQuery Mobile 下拉栏过滤具有多个关键字的记录。但是我不知道如何将这一切整合为一件事。

以下是 JQuery Mobile 下拉选项的代码:

        <label for="select-h-6a">Genre</label>
<select name="select-h-6a" id="select-h-6a">
<option value="#">Rock</option>
<option value="#">Pop</option>
<option value="#">Dance</option>
</select>
<label for="select-h-6b">Role</label>
<select name="select-h-6b" id="select-h-6b">
<option value="#">Singer</option>
<option value="#">Drummer</option>
<option value="#">Guitar</option>
</select>
<label for="select-h-6c">Members</label>
<select name="select-h-6c" id="select-h-6c">
<option value="#">1</option>
<option value="#">2</option>
<option value="#">3</option>
</select>
</fieldset>
</form>

这是我目前的代码,上面有帖子标题和帖子内容,我可以在帖子中添加更多内容,但目前只有那些选项:

 $posts = getAllPosts();

<div id="searchpost">
<div class="ui-bar ui-bar-a">
<div id="postTitle">

<?php echo $posts[$p]["post_title"];?></div>
</div>
<div class="ui-body ui-body-a">
<div id="postContent">
<?php echo $posts[$p]["post_content"];}?></div>
</div>
</div>

以及功能:

function getAllPosts() {
require "config.php";
$posts = $c->query ( "SELECT * FROM posts" );

if ( $posts->num_rows > 0 ) {

while( $row = $posts->fetch_assoc() ) {

$postData[] = array( "user_id" => $row[ "user_id" ], "post_title" => $row[ "post_title" ], "post_content" => $row[ "post_content" ] );
}
} else {
return "No Data";
}
return $postData;
}

但是,正如您所看到的,它专门调用数据库中的内容。如何才能显示我在下拉菜单中选择的内容而不是具体内容。

可以为多个选定的选项添加此选项,例如选择摇滚、歌手和 2 名成员,仅显示所有这三个人所在的记录。我想如果我不想要选项 2 中的特定内容,我可以选择所有选项它会搜索该类别中的所有内容。

所以,我需要以下方面的帮助:- 编写代码来搜索我的数据库记录并显示所有地址,解决 JQuery Mobile 下拉栏中选择的所有选项。

我希望您了解我需要什么帮助。

编辑:

我的数据库: Image

连接:(config.php)

<?php

$c = mysqli_connect( "localhost", "user", "pass", "database");

// Check connection
if (mysqli_connect_errno())
{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

?>

最佳答案

我假设这个问题是关于如何基于提交的表单数据构建 SQL 查询。如果您改用 PHP PDO 扩展,可能会更安全(而且通常更容易)。 php.net 将为您提供帮助。

转移到 PDO(强烈推荐)后,您的查询将大致如下所示。

$sql = ‘select * from posts 
where genre=:genre
and role=:role
and members=:members’;

$sth = $dbh->prepare($sql);
$sth->execute(
array(‘:genre’ => $_GET[‘genre’],
‘:role’ => $_GET[‘role’],
‘:members’=> $_GET[‘members’])
);
$result = $sth->fetchAll(); // this should be enough to echo a result

关于javascript - JQuery Mobile Mysql数据库过滤系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com