1

我正在尝试创建一个动态 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();     

    }


    ?>
4

2 回答 2

4

尝试构建 where 数组

$where = array();


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

最后通过 AND 语句 implode $where

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

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

于 2013-01-26T19:22:10.173 回答
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
*/
于 2013-01-26T18:56:21.380 回答