gpt4 book ai didi

php - 将下拉表单数据发送到php进行数据库查询

转载 作者:行者123 更新时间:2023-11-29 10:49:01 25 4
gpt4 key购买 nike

我正在尝试构建一个基于由多个下拉框组成的表单的查询。我尝试在谷歌上搜索解决方案,但未能取得任何成功,并且目前收到“数据库查询失败”消息,并且不确定这是否是因为我的查询构造错误或者表单输入未正确传输转到 php 文件。对此的任何帮助都会很棒,因为目前我真的陷入困境。

    <form action="search.php" method=post>

<select id="dung-name-select" name="dung-select">
<option name="" disabled selected hidden>Name</option>
<option value="*">All</option>
<option value="Black Rock Hold">Black Rock Hold</option>
<option value="Court of Stars">Court of Stars</option>
<option value="Darkheart Thicket">Darkheart Thicket</option>
<option value="Eye of Azshara">Eye of Azshara</option>
<option value="Halls of Valor">Halls of Valor</option>
<option value="Maw of Souls">Maw of Souls</option>
<option value="Neltharions Lair">Neltharions Lair</option>
<option value="The Arcway">The Arcway</option>
<option value="Vault of the Wardens">Vault of the Wardens</option>
</select>

<select id="dung-loc-select" name="loc-select">
<option name="" disabled selected hidden>Location</option>
<option value="*">All</option>
<option value="Valsharah">Valsharah</option>
<option value="Suramar">Suramar</option>
<option value="Azsuna">Azsuna</option>
<option value="Stormheim">Stormheim</option>
<option value="Highmountain">Highmountain</option>
</select>

<select id="dung-rating-select" name="rate-select">
<option name="" disabled selected hidden>Rating</option>
<option value="*">All</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>

<input type="submit" value="Go">

</form>

<?php

$dungeon = $_POST["dung-select"];
$location = $_POST["loc-select"];
$rate = $_POST["rate-select"];

//Database Query
$query = "SELECT * FROM dungeon where Name = $dungeon and Location = $location and Rating = $rate";
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed.");
}
?>

注意:php 代码位于表单的单独文件中。

最佳答案

您需要将变量与查询的字符串部分连接起来以得到完整的查询。所以:

$query = "SELECT * FROM dungeon where Name = ".$dungeon." and Location = ".$location." and Rating = ".$rate;

我相信你还可以使用:

$query = 'SELECT * FROM dungeon where Name = {$dungeon} and Location = {$location} and Rating = {$rate}';

无论哪种方式,除非我弄错了,您只需在查询中传递变量名称即可。

关于php - 将下拉表单数据发送到php进行数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44080126/

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