PHP 搜索数据,基于日期范围输入的 2 个表关系


PHP search data with 2 table relations based on date range input

我的代码有问题:
错误是: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 个表格关系进行搜索。

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

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

我建议您使用所有可能的组合单独测试查询(无论如何它们看起来很小)。

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

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