Php empty()将值视为空

Php empty()将值视为空,php,mysqli,Php,Mysqli,我试图按照参考资料来回答,但不确定应该如何写: empty()将$p_student中的“0”视为空= 空($_POST[“student”])?'':$_POST[“student”]$p_学生在 $\u POST[“学生”]为“0”。。。因此,以下情况始终适用 “default”,因此如果为空()则应将$p_student设置为“0”,并且 应该没问题……我想。。。(这是给p_和学生的 $p_当然有问题……) 我已经建立了一个案例陈述来匹配上面的例子,但我的问题是,根据上面的例子,应该如何编

我试图按照参考资料来回答,但不确定应该如何写:

empty()将$p_student中的“0”视为空= 空($_POST[“student”])?'':$_POST[“student”]$p_学生在 $\u POST[“学生”]为“0”。。。因此,以下情况始终适用 “default”,因此如果为空()则应将$p_student设置为“0”,并且 应该没问题……我想。。。(这是给p_和学生的 $p_当然有问题……)

我已经建立了一个案例陈述来匹配上面的例子,但我的问题是,根据上面的例子,应该如何编写$p_student

我的尝试:

// Check whether a specific student was selected
    $p_student = empty($_POST["student"])?'0':$_POST["student"];

    switch($p_student){
    case -1:
        //dont' add where filters
        break;
    default:
        $where[] = 'sa.StudentId = ?';
        $parameters[] .= $_POST["student"];
        $parameterTypes .= 'i';
    }
更新:

我试图做的是,如果
All
选项,则执行case-1,否则执行default。我是否做错了什么,因为这就是为什么我问这个关于下拉值的问题:

学生:

<select name="student" id="studentsDrop">
<option value="-1">All</option>
<option value="39">Luke Mcfadzen</option>
<option value="40">Chris Tucker</option>
</select>
<select name="question" id="questionsDrop">
<option value="-1">All</option>
<option value="72">1</option>
<option value="73">2</option>
</select>

如果您认为$_POST['student']有时可能是:

  • 0作为int
  • “”或“0”作为字符串
  • 空数组
不要使用
empty()

选中此项
(已更新)


如果ID为正,我将使用
ctype\u digit
而不是
is\u numeric
。它会拒绝诸如
1e4
4.12
,…我想你的意思是is_int()或类似的值。谢谢更新代码以反映您的建议。很抱歉,我想我该休息了:)我的意思是
ctype\u digit
,谢谢您的通知。实际上,is\u int()也是错误的。对于字符串,它不能按预期工作。更新了代码以改用filter\u var。ctype_digit可能也可以。@kufudo您好,我在尝试您的解决方案时遇到了一个
意外的T_IS_more_或_EQUAL错误
解释了被视为空的值。因此,就像下面的答案一样,如果
$\u POST
值可以是其中任何一个,请尝试
empty()
以外的其他方法。示例:
$parameters[]。=$\u POST[“student”]??将
=
更改为
=
。您没有将字符串连接到数组。@user2048994可以通过更新进行澄清。您不需要对该值调用
mysqli\u real\u escape\u string()
,因为您正在
bind\u param()
中绑定它。除了前面提到的$parameters[].=$student\u id
,您添加的代码看起来不错,应该是
$parameters[]=$student\u id;`使用
[]=
附加到数组。@user2048994上面有两个<代码>$parameters[]=$student\u id
$parameters[]=$\u POST[“学生”]这两个参数都应该是
=
,而不是
=
。@user2048994您有
$parameters[]。=$\u POST['student']
在代码顶部的
默认值中。
=
应该是
=
。我有一个下拉菜单,您可以查看上面的更新,我试图对case语句执行的是,如果用户选择
所有
选项
-1
,则不执行任何操作,否则任何其他选项将执行默认操作。使用您的if语句,我应该如何调用
All
选项值?我使用您的方法收到一个错误,但您能否查找日期并向我说明我正在做的事情?此错误
警告:mysqli\u real\u escape\u string()正好需要2个参数,1个参数在。。。第344行警告:mysqli_real_escape_string()需要两个参数,其中一个参数在。。。在第354行
$student\u id=(isset($\u POST['student']))?mysqli_real_escape_字符串($mysqli,trim($\u POST['student'])):null只需使用更新的代码这一行就可以引起问题:
$student\u id=(isset($\u POST['student'])?mysqli_real_escape_字符串($mysqli,trim($\u POST['student'])):null是否像kufudo提到的那样,它是编写过程风格而不是面向对象的风格?我尝试了更新的代码,但没有改变。
$selectedstudentanswerqry = "
    SELECT
    sa.StudentId, StudentAlias, StudentForename, ...
    FROM Student st
    ...
    ";

    // Initially empty
    $where[] = "q.SessionId = ?";
    $parameters[] = $_POST["session"];
    $parameterTypes = 'i';


    //check if POST is empty

    // Check whether a specific student was selected
//LINE 345 ERROR
$student_id = (isset($_POST['student'])) ? $mysqli->real_escape_string(trim($_POST['student'])) : null ;


if (is_numeric($student_id)){ //If student ID is a numeric value
  $where[] = "sa.StudentId = ?" ;
  $parameters[] = ((int)$student_id == -1) ? "sa.StudentId" : $student_id  ;
  $parameterTypes .= "i" ;
}

    // Check whether a specific question was selected

$question_id = (isset($_POST['question'])) ? $mysqli->real_escape_string(trim($_POST['question'])) : null ;

if (is_numeric($question_id)){ //If student ID is a numeric value
  $where[] = "q.QuestionId = ?" ;
  $parameters[] = ((int)$question_id == -1) ? "q.QuestionId" : $question_id  ;
  $parameterTypes .= "i" ;
}

    // If we added to $where in any of the conditionals, we need a WHERE clause in
    // our query
    if(!empty($where)) {
        $selectedstudentanswerqry .= ' WHERE ' . implode(' AND ', $where);
        global $mysqli;
        $selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
        // You only need to call bind_param once

        if (count($where) == 1) {
        $selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0]);
    }
    else if (count($where) == 2) {
        $selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1]);
    }
    else if (count($where) == 3) {
        $selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1], $parameters[2]);
    }

    }

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

// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute(); 
$selectedstudentanswerstmt->bind_result(...);   

$selectedstudentanswerstmt->store_result();
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows(); 
$p_student = -1;
if(isset($_POST["student"]) && filter_var($_POST["student"], FILTER_VALIDATE_INT) !== FALSE && trim($_POST["student"]) >= 0 ) {
    $p_student = $_POST["student"];
}

....
$student_id = (isset($_POST['student'])) ? mysqli_real_escape_string($mysqli, trim($_POST['student'])) : null ;

if (is_numeric($student_id)){ //If student ID is a numeric value
  $where[] = "sa.StudentId = ?" ;
  $parameters[] = ((int)$student_id == -1) ? "sa.Student_id" : $student_id  ;
  $parameterTypes .= "i" ;
}