2012-05-04 115 views
0

我有一個問題,我的代碼在這裏:
的錯誤是:Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\siix_dev\overtime\track_reports.php on line 333PHP搜索數據

我覺得在錯誤連接表的功能,但我不能分析這個案例的解決方案。

<?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表關係。

請幫我解決這個問題。 謝謝。

+0

問題出在你的SQL中。你可以顯示正在執行的日誌查詢嗎? – Marc

+0

做'$ hasil = mysql_query($ query)或者死(mysql_error())'然後測試每種類型的查詢,很快你會發現查詢有問題。你也很可能得到了SQL注入漏洞,因爲你使用了un-escpaed用戶輸入 –

+0

你可以在這裏粘貼任何mysql_error正在打印或粘貼你正在執行的最終查詢。 –

回答

0

SQL查詢設置不正確(因此會提供FALSEmysql_num_rows)。很可能在WHERE條款$bagianWhere內。從乍看起來,如果設置了$_POST['chkEmp']$_POST['chkOtdate'],它似乎會返回一個錯誤,因爲後者不是以AND開頭。

我會建議你測試查詢本身與所有可能的組合(他們似乎很小)。

此外,這裏真的需要笛卡爾產品嗎?

編輯:$bagianWhere .= "badge_id = '$badge_id' order and - 那order似乎不合適?

+0

感謝您的幫助,我已將代碼更新爲您的建議,現在仍然存在錯誤。 –

+0

你測試過每個條款嗎? –