gpt4 book ai didi

php - 如何根据在 PHP 中一次选中的多个复选框从 SQLite 3 数据库中检索值?

转载 作者:行者123 更新时间:2023-12-03 18:44:44 25 4
gpt4 key购买 nike

我是 PHP 编码的新手。我有不同的偏好供用户从我的 html 页面中选择。我想检索用户选择的所有复选框并显示其中包含所有这些偏好的酒店名称。例如,如果用户选择了“屋顶”、“海边”和“游泳池”复选框,则必须一次检索所有位于 mytrip.db 数据库中相应列中的 1 的酒店,并且得到显示。我的代码只接受一个复选框值,并仅显示酒店名称。我想显示包含所有偏好的酒店。
我的代码如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PHP: Get Values of Multiple Checked Checkboxes</title>
<link rel="stylesheet" href="css/php_checkbox.css" />
</head>
<body>
<div class="container">
<div class="main">
<h2>PHP: Get Values of Multiple Checked Checkboxes</h2>
<select id="mySelect" name="list">
<option selected>Select a place</option>
<option value="1">Lahore</option>
<option value="2">Dubai</option>
<option value="3">New York</option>
<option value="4">Canberra</option>
<option value="5">London</option>
</select>
<form action="php_checkbox.php" method="post">
<label class="heading">Select Your Preferences:</label>
<input type="checkbox" name="check_list[]" value="Swimming pool"><label>Swimming pool</label>
<input type="checkbox" name="check_list[]" value="Roof top"><label>Roof top</label>
<input type="checkbox" name="check_list[]" value="Sea side"><label>Sea side</label>



<input type="submit" name="submit" Value="Submit"/>

</html>
<?php include 'checkbox_value.php';?>
</form>
</div>
</div>
</body>
</html>

checkbox_value.php 代码如下:
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('mytrip.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {

}
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
$prefarray=array();
$i=0;
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected) {
$prefarray[$i]= $selected;

echo "<p>".$prefarray[$i] ."</p>";
echo "<p>".$selected ."</p>";
$i++;
}
echo "<p>".$i ."</p>";
$sql =<<<EOF

SELECT hotel_name from Dubai WHERE $prefarray[i]==1 AND $prefarray[i-1]==1 ;
EOF;

$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
echo "<p> <br /></p>\n";
echo "\n". $row['hotel_name'] . "\n";
}





$db->close();
echo "<br/><b>Note :</b> <span>Similarily, You Can Also Perform CRUD Operations using These Selected Values.</span>";
}
else{
echo "<b>Please Select Atleast One Option.</b>";
}
}
?>

这段代码的问题是,如果我使用查询,它只显示一个复选框值
SELECT hotel_name FROM Dubai WHERE $selected==1

我试图通过制作一个数组“prefarray”来保存选中的复选框值。但是当我执行我在我的 checkbox_value.php 代码中发布的查询时,它给了我“预置数组中 undefined offset 3”的错误。我希望数据库查询仅包含用户选择的复选框值。例如,如果用户选择了三个复选框中的两个,那么我的查询应该如下所示
SELECT hotel_name FROM Dubai WHERE $checkbox==1 AND $checkbox2==1;

任何帮助完成其中两个任务将不胜感激!

最佳答案

我已修复并重构(无法阻止自己)您的代码。

我已将列名更改为正确的列名。有一些多余的代码什么也没做,所以我把它去掉了。您要寻找的主要内容是在 foreach 循环中连接每个选定的列。我还对照硬编码 $hotelOptions 检查选定的值用于防止 sql 注入(inject)。

这是我得到的:

checkbox_value.php:

<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('mytrip.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {

}

$hotelOptions = array('swimming_pool', 'roof_top', 'sea_side');

if (isset($_POST['submit'])) {
if (!empty($_POST['check_list']) && is_array($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
$where = '';
foreach($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
if (array_search($selected, $hotelOptions) !== false) {
$where .= " AND {$selected} = 1";
}
}
$where = substr($where, 5, strlen($where));

$sql = "SELECT hotel_name FROM Dubai WHERE {$where};";

$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
echo "<p> <br /></p>\n";
echo "\n". $row['hotel_name'] . "\n";
}
$db->close();
echo "<br/><b>Note :</b> <span>Similarily, You Can Also Perform CRUD Operations using These Selected Values.</span>";
} else {
echo "<b>Please Select Atleast One Option.</b>";
}
}

html 布局:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>PHP: Get Values of Multiple Checked Checkboxes</title>
<link rel="stylesheet" href="css/php_checkbox.css"/>
</head>
<body>
<div class="container">
<div class="main">
<form action="checkbox_value.php" method="post">
<h2>PHP: Get Values of Multiple Checked Checkboxes</h2>
<select id="mySelect" name="list">
<option selected>Select a place</option>
<option value="1">Lahore</option>
<option value="2">Dubai</option>
<option value="3">New York</option>
<option value="4">Canberra</option>
<option value="5">London</option>
</select>
<p class="heading">Select Your Preferences:</p>

<input type="checkbox" name="check_list[]" value="swimming_pool" id="checkbox_1">
<label for="checkbox_1">Swimming pool</label>

<input type="checkbox" name="check_list[]" value="roof_top" id="checkbox_2">
<label for="checkbox_2">Roof top</label>

<input type="checkbox" name="check_list[]" value="sea_side" id="checkbox_3">
<label for="checkbox_3">Sea side</label>

<div>
<input type="submit" name="submit" Value="Submit"/>
</div>
<?php include 'checkbox_value.php';?>
</form>
</div>
</div>
</body>
</html>

关于php - 如何根据在 PHP 中一次选中的多个复选框从 SQLite 3 数据库中检索值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43360812/

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