【问题标题】:PHP/MySQL: SQL Statement from different columnsPHP/MySQL:来自不同列的 SQL 语句
【发布时间】:2015-05-22 09:26:42
【问题描述】:

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

我有一个表“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.

但我试图找到一个解决方案,让我得到所有存在“能量”和“中音”的命中,无论它们出现在 Pot1、Pot2、Pot3 还是 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

【问题讨论】:

  • 我猜你在寻找 OR 而不是 AND??

标签: php mysql sql operator-keyword


【解决方案1】:

您正在尝试获得“能量”或“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]];
    } 

} 

【讨论】:

  • 感谢您的帮助 henrik。但是这段代码不能正常工作。此代码创建此 SQL 语句,它在 MySQL "SELECT * FROM favcolor WHERE blue=1 and (pot1=)" >> 看起来他没有接管 $limonades[$i] 在这行中的值代码:“$sql .= $keys[$i].' = '.$limonades[$i];"
  • 用$limonades[$keys[$i]]替换$limonades[$i]
猜你喜欢
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
相关资源
最近更新 更多