控件没有进入我的PHP代码中的else-if块

时间:2016-12-08 11:28:10

标签: php html mysql forms if-statement

这是我的PHP代码,它从html表单获取用户信息并从数据库中获取数据。 不幸的是, 如果块.it只是显示数据库中的所有行,或者什么都没有,则控件不会进入else。

{
<?php
    include("config.php");
    echo "<form class='navbar-form' method='POST' action='#'>"; 

    $sqluser="SELECT DISTINCT username FROM user ORDER BY username ASC";
    $resultuser=mysqli_query($conn,$sqluser);

    echo "<div class='form-group' style='margin-right:5px;'><input  name='from_date' type='date' data-date-inline-picker='true' /></div>";
    echo "<div class='form-group' style='margin-right:5px;'><input  name='to_date' type='date' data-date-inline-picker='true' /></div>";
    echo "<div class='form-group' style='margin-right:5px;'><select  name='username' style='width:170px;height:33px;'>";
    echo "<option select='selected'>-- Username --</option>";
    while($ro1=mysqli_fetch_array($resultuser)){
        echo "<option value='".$ro1['username']."'>".$ro1['username']."</option>";
    }
    echo "</select>";
    echo "</div>";
    echo "<div class='form-group' style='margin-right:5px;'><input type='submit' value='submit'></div>";

    echo "</form>";

    if(isset($_POST['username']))
    {
        $username=$_POST['username'];

        echo " <div align='center' style='margin-top:50px;'>";

        $sql="SELECT user.username,user.designation,
                    checkin.logdate,checkin.logintime,
                    checkin.loginhostname,checkin.loginipaddress,
                    checkin.logindevicetype,checkout.logouttime,
                    checkout.logouthostname,checkout.logoutipaddress,
                    checkout.logoutdevicetype 
                    FROM user 
                        INNER JOIN checkin ON user.username=checkin.username 
                        LEFT JOIN checkout ON user.username=checkout.username 
                                AND checkin.session_id=checkout.session_id 
                    ORDER BY checkin.ltime_stamp DESC LIMIT 10";

        $result=mysqli_query($conn,$sql);
        if($result->num_rows === 0){
            echo "<h4> No Records Found ! </b4>";
        } else {
            echo "<div style='overflow-x:auto;'>";  
            echo "<table border='1'>";
            echo "<tr>";
            echo "<th>&nbsp&nbsp Date &nbsp&nbsp</th>";
            echo "<th>&nbsp Username &nbsp&nbsp</th>";
            echo "<th>&nbsp Designation &nbsp&nbsp</th>";
            echo "<th>&nbsp CheckInTime &nbsp&nbsp</th>";
            echo "<th>&nbsp&nbsp&nbsp&nbsp&nbsp Hostname &nbsp&nbsp</th>";
            echo "<th>&nbsp Ipaddress &nbsp&nbsp</th>";
            echo "<th>&nbsp DeviceType &nbsp&nbsp</th>";
            echo "<th>&nbsp CheckOutTime &nbsp&nbsp</th>";
            echo "<th>&nbsp&nbsp&nbsp&nbsp&nbsp Hostname &nbsp&nbsp</th>";
            echo "<th>&nbsp Ipaddress &nbsp&nbsp</th>";
            echo "<th>&nbsp DeviceType &nbsp&nbsp</th>";
            echo "</tr>";

            while($row=mysqli_fetch_array($result))
            {
                echo "<tr>";
                echo "<td>".$row['logdate']."</td>";
                echo "<td>".$row['username']."</td>";
                echo "<td>".$row['designation']."</td>";
                echo "<td>".$row['logintime']."</td>";
                echo "<td>".$row['loginhostname']."</td>";
                echo "<td>".$row['loginipaddress']."</td>";
                echo "<td>".$row['logindevicetype']."</td>";
                echo "<td>".$row['logouttime']."</td>";
                echo "<td>".$row['logouthostname']."</td>";
                echo "<td>".$row['logoutipaddress']."</td>";
                echo "<td>".$row['logoutdevicetype']."</td>";
                echo "</tr>";

            }
            echo "</table>";
        }

        echo "</div>";
        echo "</div>";
    }
    elseif(isset($_POST['from_date'],$_POST['to_date'])){
        $from_date=date('Y-m-d',$_POST['from_date']);
        $to_date=date('Y-m-d',$_POST['to_date']);

        $sql="SELECT user.username,user.designation,checkin.logdate,
                    checkin.logintime,checkin.loginhostname,
                    checkin.loginipaddress,checkin.logindevicetype,
                    checkout.logouttime,checkout.logouthostname,
                    checkout.logoutipaddress,checkout.logoutdevicetype 
                    FROM user 
                        INNER JOIN checkin ON user.username=checkin.username 
                        LEFT JOIN checkout ON user.username=checkout.username 
                                AND checkin.session_id=checkout.session_id BETWEEN '$from_date' and '$to_date' 
                        ORDER BY checkin.ltime_stamp DESC 
                        LIMIT 10";

        echo $sql;
        echo $result->num_rows;

        $result=mysqli_query($conn,$sql);

        echo " <div align='center' style='margin-top:50px;'>";
        if($result->num_rows === 0){
            echo "<h4> No Records Found ! </b4>";
        } else {
            echo "<div style='overflow-x:auto;'>";  
            echo "<table border='1'>";
            echo "<tr>";
            echo "<th>&nbsp&nbsp Date &nbsp&nbsp</th>";
            echo "<th>&nbsp Username &nbsp&nbsp</th>";
            echo "<th>&nbsp Designation &nbsp&nbsp</th>";
            echo "<th>&nbsp CheckInTime &nbsp&nbsp</th>";
            echo "<th>&nbsp&nbsp&nbsp&nbsp&nbsp Hostname &nbsp&nbsp</th>";
            echo "<th>&nbsp Ipaddress &nbsp&nbsp</th>";
            echo "<th>&nbsp DeviceType &nbsp&nbsp</th>";
            echo "<th>&nbsp CheckOutTime &nbsp&nbsp</th>";
            echo "<th>&nbsp&nbsp&nbsp&nbsp&nbsp Hostname &nbsp&nbsp</th>";
            echo "<th>&nbsp Ipaddress &nbsp&nbsp</th>";
            echo "<th>&nbsp DeviceType &nbsp&nbsp</th>";
            echo "</tr>";

            while($row=mysqli_fetch_array($result))
            {
                echo "<tr>";
                echo "<td>".$row['logdate']."</td>";
                echo "<td>".$row['username']."</td>";
                echo "<td>".$row['designation']."</td>";
                echo "<td>".$row['logintime']."</td>";
                echo "<td>".$row['loginhostname']."</td>";
                echo "<td>".$row['loginipaddress']."</td>";
                echo "<td>".$row['logindevicetype']."</td>";
                echo "<td>".$row['logouttime']."</td>";
                echo "<td>".$row['logouthostname']."</td>";
                echo "<td>".$row['logoutipaddress']."</td>";
                echo "<td>".$row['logoutdevicetype']."</td>";
                echo "</tr>";

            }
            echo "</table>";
        }
        echo "</div>";
        echo "</div>";
    } else {
        echo " <div align='center' style='margin-top:50px;'>";
        echo "<h3> Please provide data To Display User Information! </h3>";
        echo "</div>";
    }
?>
    }

表单包含3个输入字段,

1) from date 
2) to date 
3)username. 

如果我只输入用户名,它应该从db获取所选用户的数据。如果我从n进入日期,它会从db中获取给定范围之间的数据。但问题是如果我从n进入日期,它是从db.even获取所有行,如果我给出db表中没有的日期

0 个答案:

没有答案