Home » Php » PHP & MYSQL: Why mysql cannot read !empty?

PHP & MYSQL: Why mysql cannot read !empty?

Posted by: admin June 30, 2018 Leave a comment

Questions:

i’m trying to search multiple fields in database where the user also can search just for one input. But now, the problem is sql don’t want to read the (!empty) in if else condition (if i just want to search 1 input). BUt then if i fill all the searching field, it displays well.
Please correct if i do wrong. TQSM!

so, here is my code:

if(isset($_POST['search4']))
{
    $tarikh = $_POST['tahun'];
    $tempat= $_POST['tempat'];
    $sesi = $_POST['sesi'];
    $status = $_POST['status'];

    $query = "SELECT * FROM tempahanbilik WHERE status!='BELUM DIPROSES' AND kosong = 'tidak' AND MONTH(tarikh) = 5 AND YEAR(tarikh) = YEAR(CURDATE()) AND YEAR(tarikh2) = YEAR(CURDATE()) ";
    if($tarikh && !empty($tarikh))
    {
        $query .="AND YEAR(tarikh) = $tarikh AND YEAR(tarikh2) = $tarikh";
    }
    if($tempat && !empty($tempat))
    {
        $query .="AND  tempat = '$tempat'";
    }
    if($sesi && !empty($sesi))
    {
        $query .="AND masa = '$sesi'";
    }
    if($status && !empty($status))
    {
        $query .="AND status = '$status'";
    }

    $query .= "ORDER BY tarikh AND tarikh2 DESC";
    $result = $mysqli->query($query) or die($mysqli->error._LINE_);
    $total = $result->num_rows;

    if($total>0)
    {
        $total =1;
    while($row = $result->fetch_assoc())
    {       
            $kp = $row['PenggunanoKP'];
                    $a = "SELECT * FROM pengguna WHERE PenggunanoKP = '$kp'";
                    $results = $mysqli->query($a) or die($mysqli->error._LINE_);
                 $rows = $results->fetch_assoc();

            //display the informations
    }               
} 
else
{   
    echo"  
    <tr>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td> 
      <td>-</td>
      <td>-</td>
      <td>-</td> 
      <td>-</td>
    </tr>\n";        
}
Answers: