如何创建动态WHERE子句

时间:2013-01-26 18:30:11

标签: php mysqli

我正在尝试创建一个动态WHERE子句,根据从下拉菜单中选择的选项,它将编译正确的WHERE子句。但我不认为我这样做是正确的。

首先应该有一个默认的WHERE子句,无论从下拉菜单中选择哪个选项,都应该有一个WHERE子句检查选定的SessionId所以这应该是SessionId = ?

然后,根据从下拉菜单中选择的选项,它将编译WHERE子句中的其他字段。有两个下拉菜单适用于StudentsQuestions。可能的结果是:

Student selected != 'All':添加StudentId =?在WHERE子句中 Student selected == 'All':删除StudentId =?来自WHERE子句 Question selected != 'All':添加QuestionId =?在WHERE子句中 Question selected == 'All':删除QuestionId =?来自WHERE子句

我的问题是我该如何设置?

以下是我目前的情况:

        if(isset($_POST['answerSubmit'])) // we have subbmited the third form
        {

    $selectedstudentanswerqry = "
    SELECT
    StudentAlias, StudentForename, StudentSurname, q.SessionId, QuestionNo, QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT( DISTINCT Answer
    ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType, QuestionMarks, 
    GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime, MouseClick, StudentMark
    FROM Student s
    INNER JOIN Student_Answer sa ON (s.StudentId = sa.StudentId)
    INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId)
    INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
    INNER JOIN Answer an ON q.QuestionId = an.QuestionId
    LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
    LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
    ";

    if ($_POST['student'] != 'All'){
    $selectedstudentanswerqry .= "
    WHERE (SessionId = ? AND StudentId = ?)
    ";
    }

    if ($_POST['question'] != 'All'){
    $selectedstudentanswerqry .= "
    WHERE (SessionId = ? AND QuestionId = ?)
    ";
    }

    $selectedstudentanswerqry .= "
    GROUP BY sa.StudentId, q.QuestionId
    ORDER BY StudentAlias, q.SessionId, QuestionNo
    ";

    global $mysqli;
    $selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
    if ($_POST['student'] != 'All'){
    // You only need to call bind_param once
    $selectedstudentanswerstmt->bind_param("ii",$_POST["session"],$_POST["student"]);
    }
    if ($_POST['question'] != 'All'){
    // You only need to call bind_param once
    $selectedstudentanswerstmt->bind_param("ii",$_POST["session"],$_POST["question"]);
    }
    // get result and assign variables (prefix with db)
    $selectedstudentanswerstmt->execute(); 
    $selectedstudentanswerstmt->bind_result($detailsStudentAlias,$detailsStudentForename,$detailsStudentSurname,$detailsSessionId,$detailsQuestionNo, 
    $detailsQuestonContent,$detailsOptionType,$detailsNoofAnswers,$detailsAnswer,$detailsReplyType,$detailsQuestionMarks,$detailsStudentAnswer,$detailsResponseTime,
    $detailsMouseClick,$detailsStudentMark);
    $selectedstudentanswerstmt->store_result();
    $selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();     

    }


    ?>

2 个答案:

答案 0 :(得分:5)

尝试构建数组

$where = array();


if ((int) $studentID >0) {
   $where[] = " studentID = '{$studentID}' ";
}
if ((int) $QuestionId  >0) {
   $where[] = " QuestionId  = '{$QuestionId }' ";
}

并在结束时通过AND statment

破坏$ where
if (!empty($where)) 
$query['where'] = ' WHERE '. implode(' AND ', $where);

这只是一种方式。我没有调试这段代码。

答案 1 :(得分:0)

您的案件:

- Student selected != 'All' : Add StudentId = ? in WHERE clause 
- Student selected == 'All' : Remove StudentId = ? from WHERE clause 
- Question selected != 'All' : Add QuestionId = ? in WHERE clause 
- Question selected == 'All' : Remove QuestionId = ? from WHERE clause

<?php
$selectedstudentanswerqry = "WHERE SessionId = ? ";
if ($_POST['student'] != 'All'){
 $selectedstudentanswerqry .= " and StudentId = ? ";
}
else{
/*
$selectedstudentanswerqry .= "
//what is condition for if student == all ?
";
*/
}
if ($_POST['question'] != 'All'){
  $selectedstudentanswerqry .= " and QuestionId = ? ";
}
else{
}
?>

考虑

student = 1 then:

if student != All = true
if question != AA = true

student = All
if student != All = false
if question != AA = true

question = 1 
if student != All = true
if question != AA = true

question = All
if student != All = true
if question != AA = false

检查你是否想要这个?我想不。

    //case1
    if ($_POST['student'] != 'All'){
      $selectedstudentanswerqry .= "
      WHERE (SessionId = ? AND StudentId = ?)
      ";
    }

    //case2
    if ($_POST['question'] != 'All'){
      //case 2.1
      if ($_POST['student'] != 'All'){
        $selectedstudentanswerqry .= "
         and  (QuestionId = ?)
        ";
      }
      //case 2.2
      else{
        $selectedstudentanswerqry .= "
        WHERE (SessionId = ? AND QuestionId = ?)
        ";
      }

    }
/*
    testing
    1- student != All, question != All
    case1: true
    case1: result: $selectedstudentanswerqry = WHERE (SessionId = ? AND StudentId = ?)
    case2 : true
    case 2.1: true
    case2.1 result:  $selectedstudentanswerqry .= and  (QuestionId = ?)

    2- student != All question = All
    case1: true
    case1: result: $selectedstudentanswerqry = WHERE (SessionId = ? AND StudentId = ?)
    case2: false

    3- student = All question != All
    case1: false
    case2: true
    case2.1: false
    case2.2: true
    case2.2 result: $selectedstudentanswerqry = WHERE (SessionId = ? AND QuestionId = ?)

    4- student = All question = All
    case1: false
    case2: false
*/