如何设置测试变量,不是null并且具有特定值

时间:2017-08-27 15:37:01

标签: php

我有问题。 我已经制作了一个PHP代码,用于通过检查一个文本框中的值来选择数据。如果值存在,它将使用我创建的函数。我不知道为什么我的代码不会起作用。 我试图更改和更改我的代码,但它仍无效。

这是我的代码 -



<?php
include("../../Connections/koneksi.php");

$date_awal=$_POST['date_start'];
$date_akhir=$_POST['date_end'];
$kode=$_POST['kode_mat'];
$kode_mat=$_POST['kode_mat1'];
$sloc=$_POST['s_loc'];
$s_loc=$_POST['s_loc1'];
$type=$_POST['get_type'];

//Display all data
$rows = array();

if($date_awal != '' && $kode != '' && isset($type)== '-' ){
 //data with input date_awal and kode
 $sql = "SELECT * FROM wjm WHERE date='$date_awal' AND kode='$kode' AND uses IN ('Borongan','Tilting') order by date,kode asc";
}else if ( $date_awal != '' && isset($type)== '-' ){
 //data with input date_awal
 $sql = "SELECT * FROM wjm WHERE date='$date_awal' AND uses IN ('Borongan','Tilting') order by date,kode asc ";
}else if ($date_awal !='' && isset($type)== 'all'){
 //data with input date_awal and type=all
 $sql = "SELECT * FROM wjm WHERE date='$date_awal' AND uses IN ('Borongan','Tilting') order by date,kode asc ";
}else if ($date_awal !='' && isset($type)== 'report'){
 //data with input date_awal and type=report
 $sql = "SELECT * FROM wjm WHERE date='$date_awal' AND uses IN ('Borongan') order by date,kode asc ";
}else{
 //data no input
 $sql = "SELECT * FROM wjm WHERE uses IN ('Borongan','Tilting') ORDER by date,kode asc";
}

$query = mysqli_query($db,$sql);

while($tmp= mysqli_fetch_assoc($query)) {
    $rows[] = $tmp;
}

echo json_encode($rows);

mysqli_close($db);
?> 
&#13;
&#13;
&#13;

当我将$type上的值设置为report时,它仍会在$type "-" and "all"

的函数处呈现

2 个答案:

答案 0 :(得分:0)

您检查功能

  

isset()

尝试使用该功能

  

empty()

     

!空()

     

的strlen($变种)大于0

因为$ type总是isset,empty或full ... isset()将始终返回true。

答案 1 :(得分:0)

使用isset检查非null并将条件应用于$ type,因为@ chris85你不能组合条件:

<?php
include("../../Connections/koneksi.php");

$date_awal=$_POST['date_start'];
$date_akhir=$_POST['date_end'];
$kode=$_POST['kode_mat'];
$kode_mat=$_POST['kode_mat1'];
$sloc=$_POST['s_loc'];
$s_loc=$_POST['s_loc1'];
$type=$_POST['get_type'];

//Display all data
$rows = array();

if($date_awal != '' && $kode != '' && isset($type) &&$type== '-' ){
 //data with input date_awal and kode
 $sql = "SELECT * FROM wjm WHERE date='$date_awal' AND kode='$kode' AND uses IN ('Borongan','Tilting') order by date,kode asc";
}else if ( $date_awal != '' && isset($type) &&$type == '-' ){
 //data with input date_awal
 $sql = "SELECT * FROM wjm WHERE date='$date_awal' AND uses IN ('Borongan','Tilting') order by date,kode asc ";
}else if ($date_awal !='' && isset($type) && $type== 'all'){
 //data with input date_awal and type=all
 $sql = "SELECT * FROM wjm WHERE date='$date_awal' AND uses IN ('Borongan','Tilting') order by date,kode asc ";
}else if ($date_awal !='' && isset($type) && $type== 'report'){
 //data with input date_awal and type=report
 $sql = "SELECT * FROM wjm WHERE date='$date_awal' AND uses IN ('Borongan') order by date,kode asc ";
}else{
 //data no input
 $sql = "SELECT * FROM wjm WHERE uses IN ('Borongan','Tilting') ORDER by date,kode asc";
}

$query = mysqli_query($db,$sql);

while($tmp= mysqli_fetch_assoc($query)) {
    $rows[] = $tmp;
}

echo json_encode($rows);

mysqli_close($db);
?>