PHP根据日期范围输入搜索具有2个表关系的数据

时间:2012-05-04 01:21:45

标签: php mysql

我的代码存在问题:
错误是:Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\siix_dev\overtime\track_reports.php on line 333

我认为联接表函数中的错误,但我无法分析这种情况下的解决方案。

<?php
                    include ("config.php");

                    $bagianWhere = "";

                    if (isset($_POST['chkBadge']))
                    {
                        $badge_id = $_POST['badge_id'];
                        $status_acknowledge = "Acknowledged";
                        if (empty($bagianWhere))
                        {
                            $bagianWhere .= "badge_id = '$badge_id' order and t_submissions.submission_no=t_acknowledged.submission_no and t_acknowledged.status_acknowledge='$status_acknowledge' by time_submission DESC";
                        }
                    }

                    if (isset($_POST['chkEmp']))
                    {
                       $employee_name = $_POST['employee_name'];
                       $status_acknowledge = "Acknowledged";
                       if (empty($bagianWhere))
                        {
                            $bagianWhere .= "employee_name LIKE '$employee_name' and t_submissions.submission_no=t_acknowledged.submission_no and t_acknowledged.status_acknowledge='$status_acknowledge' order by time_submission DESC";
                        }
                        else
                        {
                            $bagianWhere .= " AND employee_name LIKE '$employee_name' and t_submissions.submission_no=t_acknowledged.submission_no and t_acknowledged.status_acknowledge='$status_acknowledge' order by time_submission DESC";
                        }
                    }

                    if (isset($_POST['chkOtdate']))
                    {
                        $date_from = $_POST['date_from'];
                        $date_to = $_POST['date_to'];
                        $status_acknowledge = "Acknowledged";

                        if (empty($bagianWhere))
                        {
                            $bagianWhere .= "ot_date between '$date_from' and '$date_to' and t_submissions.submission_no=t_acknowledged.submission_no and t_acknowledged.status_acknowledge='$status_acknowledge' order by time_submission DESC";
                        }
                        else
                        {
                            $bagianWhere .= " AND ot_date between '$date_from' and '$date_to' and t_submissions.submission_no=t_acknowledged.submission_no and t_acknowledged.status_acknowledge='$status_acknowledge' order by time_submission DESC";
                        }
                    }

                    $query = "SELECT t_submissions.submission_no, t_submissions.badge_id,
                    t_submissions.employee_name, t_submissions.ot_date, t_submissions.dept_name,
                    t_submissions.ot_from, t_submissions.ot_to,
                    t_submissions.remarks, t_submissions.submission_by, t_acknowledged.acknowledge_by,

                    FROM t_submissions, t_acknowledged WHERE ".$bagianWhere;

                    $hasil = mysql_query($query);
                                if(mysql_num_rows($hasil) > 0)
                                {
                                    ?>
                                    <div class="outer">
                                        <div id="main" class="wrapper">
                                            <div class="content-area">
                                            <table cellspacing="0" class="font">
                                                <thead>
                                                    <tr>
                                                        <th class="th">Form No</th>
                                                        <th class="th">Badge ID</th>
                                                        <th class="th">Name</th>
                                                        <th class="th">OT Date</th>
                                                        <th class="th">OT From</th>
                                                        <th class="th">OT To</th>
                                                        <th class="th">Submission By</th>
                                                        <th class="th">Status Ack.</th>
                                                        <th class="th">Status App.</th>
                                                    </tr>                                               
                                                </thead>

                                                <?php
                                                    while($submission = mysql_fetch_array($hasil))
                                                    {?>
                                                        <tbody>
                                                            <tr>
                                                                <td class="td"><?php echo $submission['submission_no'];?></td>
                                                                <td class="td"><?php echo $submission['badge_id'];?></td>
                                                                <td class="td"><?php echo $submission['employee_name'];?></td>
                                                                <td class="td"><?php echo $submission['ot_date'];?></td>
                                                                <td class="td"><?php echo $submission['ot_from'];?></td>
                                                                <td class="td"><?php echo $submission['ot_to'];?></td>
                                                                <td class="td"><?php echo $submission['submission_by'];?></td>
                                                                <td class="td"><?php echo $submission['acknowledge_by'];?></td>
                                                                <td class="td"><?php echo $submission['approval_by'];?></td>
                                                            </tr>
                                                        </tbody>
                                                    <?php 
                                                    }?>
                                            </table>
                                            </div>
                                        </div>
                                    </div>
                                    <?php
                                }
                                else
                                {
                                    echo '<p STYLE="position:absolute; TOP:170px; left:500px;">Data not found.</p>';
                                }
                    ;
                    ?>

使用日期范围输入和2个表关系进行搜索。

请帮我解决这个问题。 谢谢。

1 个答案:

答案 0 :(得分:0)

SQL查询设置不正确(因此向FALSE提供mysql_num_rows)。最有可能在WHERE条款$bagianWhere中。乍一看似乎如果设置$_POST['chkEmp']$_POST['chkOtdate'],它将返回错误,因为后者不以AND开头。

我建议您使用所有可能的组合自己测试查询(它们看起来很小)。

此外,这里真的需要笛卡尔产品吗?

修改:$bagianWhere .= "badge_id = '$badge_id' order and - order似乎不合适?