我有一个数据库,该数据库有一个名为shapes的行,并且包含带有矩形,正方形和圆形的条目。我目前有以下代码来仅获取用户要求的形状。 (当前是矩形和圆形,正方形和圆形或仅是圆形)。由于某种原因,它无法正常工作并引发数据库错误。谁能帮我?
“ MEMBER_FROM”是他们所在的列

$where = "";
    if($context['no_sq'] == 1 && $context['no_rec'] == 0) {
        $where .= " WHERE MEMBER_FROM != 'Square'";
    } else if($context['no_sq'] == 1 && $context['no_rec'] == 0) {
        $where .= " WHERE MEMBER_FROM != 'Rectangle'";
    } else if($context['no_sq'] == 1 && $context['no_rec'] == 1) {
        $where .= " WHERE MEMBER_FROM != 'Square' AND MEMBER_FROM != 'Rectangle'";
    }
    $request = mysql_query("SELECT ID_SHAPES FROM {$db_prefix}shapes WHERE ID_MEMBER = {$memID}{$where}", __FILE__, __LINE__);

最佳答案

你不能说两次。使用AND ...

   $where = "";
        if($context['no_squ'] == 1 && $context['no_rec'] == 0) {
            $where .= " AND MEMBER_FROM != 'Square'";
        } else if($context['no_sq'] == 1 && $context['no_rec'] == 0) {
            $where .= " AND MEMBER_FROM != 'Rectangle'";
        } else if($context['no_sq'] == 1 && $context['no_rec'] == 1) {
            $where .= " AND MEMBER_FROM != 'Square' AND MEMBER_FROM != 'Rectangle'";
        }

     $request = mysql_query("SELECT ID_SHAPES FROM {$db_prefix}shapes WHERE ID_MEMBER = {$memID}{$where}", __FILE__, __LINE__);


更新:
根据您的声明形状是排除复选框,在数组中命名这些框然后基于循环构建查询会更有意义。否则,此代码可能很难维护...

<input type="checkbox" name="exclude[]" value="Square"> Square
<input type="checkbox" name="exclude[]" value="Rectangle"> Rectangle
<input type="checkbox" name="exclude[]" value="Circle"> Circle


然后在PHP中

foreach($_POST['exclude'] AS $exclude)
{
    $where .= " AND MEMBER_FROM != '".$exclude."'";
}


...显然转义了$ exclude var,但为简单起见,我们假设您转义了用户输入。

关于mysql - MySQL:根据值排除列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12118023/

10-13 05:46