gpt4 book ai didi

PHP/MySQL : SQL Statement from different columns

转载 作者:行者123 更新时间:2023-11-30 22:47:38 26 4
gpt4 key购买 nike

我有一种行为,我很困惑如何解决这个问题......

我有一个表“favcolor”,其中有 4 个列,名称分别为“pot1”、“pot2”、“pot3”、“pot4”。此列中是饮料的 ID。

当我在“selectbox1”(“limonade”)中选择饮料时:Energy/value="5"。在“selectbox2”(“limonade2”)中,我选择饮料:Mezzo/value="3"。它生成此 SQL 语句:

 "SELECT * FROM favcolor WHERE pot1= 5 AND pot2= 3" - i get 15 hits.

现在我在“selectbox1”(“limonade”)中选择饮料:Mezzo/value="3"。在“selectbox2”(“limonade2”)中,我选择饮料:Energy/value="5"。现在生成了这条 SQL 语句:

 "SELECT * FROM favcolor WHERE pot1= 3 AND pot2= 5" - i get 7 hits.

But I try to find a solution that i get all hits where "Energy" and "Mezzo" exists, regardless of whether they appear in Pot1, Pot2, Pot3 or Pot4.

我希望你能帮我找到这个“问题”的解决方案。

为了更好地理解这里是我当前的代码:

<form method="post"action="./usercolor.php">

<input type="checkbox" id="colorred" name="colorred" value="1"/>red</td>
<input type="checkbox" id="colorblue" name="colorblue" value="1"/>blue</td>
<input type="checkbox" id="coloryellow" name="coloryellow" value="1"/>yellow</td>
<input type="checkbox" id="colorgreen" name="colorgreen" value="1"/>green</td>
<input type="checkbox" id="colorblack" name="colorblack" value="1"/>black</td>

</select name="limonade">
<option value="1">Cola</option>
<option value="2">Fanta</option>
<option value="3">Mezzo</option>
<option value="4">Sprite</option>
<option value="5">Energy</option>
</select>
</select name="limonade2">
<option value="1">Cola</option>
<option value="2">Fanta</option>
<option value="3">Mezzo</option>
<option value="4">Sprite</option>
<option value="5">Energy</option>
</select>
</select name="limonade3">
<option value="1">Cola</option>
<option value="2">Fanta</option>
<option value="3">Mezzo</option>
<option value="4">Sprite</option>
<option value="5">Energy</option>
</select>
</select name="limonade4">
<option value="1">Cola</option>
<option value="2">Fanta</option>
<option value="3">Mezzo</option>
<option value="4">Sprite</option>
<option value="5">Energy</option>
</select>

<input type="submit" value="SEARCH"/>

</form>

PHP代码:

$whereArr = Array();
if (isset($_POST["colorred"]) && $_POST["colorred"]==1) $whereArr[] = "red=1";
if (isset($_POST["colorblue"]) && $_POST["colorblue"]==1) $whereArr[] = "blue=1";
if (isset($_POST["colorgreen"]) && $_POST["colorgreen"]==1) $whereArr[] = "green=1";
if (isset($_POST["coloryellow"]) && $_POST["coloryellow"]==1) $whereArr[] = "yellow=1";
if (isset($_POST["colorblack"]) && $_POST["colorblack"]==1) $whereArr[] = "black=1";


$sql = "SELECT * FROM favcolor WHERE " . implode(" AND ", $whereArr);


if (empty($whereArr)) {
if( $_POST["limonade"] > 0 ) $sql .= ' pot1 = ' . $_POST["limonade"];
if( $_POST["limonade2"] > 0 ) $sql .= ' pot2 = ' . $_POST["limonade2"];
if( $_POST["limonade3"] > 0 ) $sql .= ' pot3 = ' . $_POST["limonade3"];
if( $_POST["limonade4"] > 0 ) $sql .= ' pot4 = ' . $_POST["limonade4"];
}
else {
if( $_POST["limonade"] > 0 ) $sql .= ' and pot1 = ' . $_POST["limonade"];
if( $_POST["limonade2"] > 0 ) $sql .= ' and pot2 = ' . $_POST["limonade2"];
if( $_POST["limonade3"] > 0 ) $sql .= ' and pot3 = ' . $_POST["limonade3"];
if( $_POST["limonade4"] > 0 ) $sql .= ' and pot4 = ' . $_POST["limonade4"];
}

表格:

 User   |Red    |Blue   |Yellow |Green  |Black
Klaus |0 |1 |1 |0 |0
Jessy |1 |0 |1 |0 |1
Andy |1 |1 |0 |0 |0
Alex |0 |0 |0 |1 |1
Denis |1 |1 |0 |0 |1

最佳答案

您正在尝试购买“Energy”或“Mezzo”饮料:

$limonades = array():
if( $_POST["limonade"] > 0 ) $limonades['pot1'] = $_POST["limonade"];
if( $_POST["limonade2"] > 0 ) $limonades['pot2'] = $_POST["limonade2"];
if( $_POST["limonade3"] > 0 ) $limonades['pot3'] = $_POST["limonade3"];
if( $_POST["limonade4"] > 0 ) $limonades['pot4'] = $_POST["limonade4"];
if(!empty($limonades)) {
if (!empty($whereArr))
$sql .= ' and ';
$sql .= '(';
$keys = array_keys($limonades);
for($i = 0; $i < count($limonades); $i++) {
if($i == 0)
$sql .= $keys[$i].' = '. $limonades[$keys[$i]];
elseif($i == count($limonades) - 1)
$sql .= ')';
else
$sql .= ' OR '.$keys[$i].' = '. $limonades[$keys[$i]];
}

}

关于PHP/MySQL : SQL Statement from different columns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29141646/

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