SQL 来自数据库的语句
SQL Statement from database
我不是 SQL 中的 "pro"。
我有以下场景:
Table: 最喜欢的颜色
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
HTML代码
<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>
<input type="submit" value="SEARCH" /> </form>
PHP/SQL(使用MySQL 5.6):
$colorred = $_POST["colorred"];
$colorblue = $_POST["colorblue"];
$colorgreen = $_POST["colorgreen"];
$coloryellow = $_POST["coloryellow"];
$colorblack = $_POST["colorblack"];
SELECT * FROM favcolor WHERE red = '".$colorred."' AND blue = '".$colorblue."' AND green = '".$colorgreen."' AND yellow = '".$coloryellow."' AND black = '".$colorblack."'
现在解决我的问题:
例如我想知道哪个用户喜欢黄色。然后我选中 id "coloryellow" 上的复选框并按 "SEARCH"。
但是我没有收到任何结果,因为 SQL 语句中的 "AND" 运算符是错误的...
我必须使用哪个运算符才能让我的 SQL 语句给我两个结果?
$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";
$sqlStr = "SELECT * FROM favcolor WHERE " . implode(" AND ", $whereArr);
然后你运行查询
我不是 SQL 中的 "pro"。
我有以下场景:
Table: 最喜欢的颜色
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
HTML代码
<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>
<input type="submit" value="SEARCH" /> </form>
PHP/SQL(使用MySQL 5.6):
$colorred = $_POST["colorred"];
$colorblue = $_POST["colorblue"];
$colorgreen = $_POST["colorgreen"];
$coloryellow = $_POST["coloryellow"];
$colorblack = $_POST["colorblack"];
SELECT * FROM favcolor WHERE red = '".$colorred."' AND blue = '".$colorblue."' AND green = '".$colorgreen."' AND yellow = '".$coloryellow."' AND black = '".$colorblack."'
现在解决我的问题: 例如我想知道哪个用户喜欢黄色。然后我选中 id "coloryellow" 上的复选框并按 "SEARCH"。 但是我没有收到任何结果,因为 SQL 语句中的 "AND" 运算符是错误的... 我必须使用哪个运算符才能让我的 SQL 语句给我两个结果?
$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";
$sqlStr = "SELECT * FROM favcolor WHERE " . implode(" AND ", $whereArr);
然后你运行查询