Rob Sutan Rob Sutan - 2 months ago 17
PHP Question

How to test a variable is set, not null and has a particular value

I have a problem.
I have made a php code for selecting data by checking a value from one textbox. If value is exist it will use a function that I made. I don't know why my code won't work.
I have tried to change and change my code and still it does not work.

This is my code-



<?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);
?>





the eror is when i set value on
$type
as
report
it still render at the function with
$type "-" and "all"

Answer Source

Use isset to check not null and apply condition to $type, as @chris85 you cannot combine the conditions:

<?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);
?>