2015-10-06 36 views
0

如果找到記錄,則SearchText有效,但當找不到記錄時會出現錯誤。 以下是錯誤。注意:未定義的變量:sample_list中的第166行的C:\ xampp \ htdocs \ viewsample \ samplelist.php 警告:爲166行的C:\ xampp \ htdocs \ viewsample \ samplelist.php中的foreach()提供的無效參數 如果我再次點擊搜索按鈕,錯誤消失。 如果找不到記錄,是否可以返回主頁? 我在這裏搜索了一個可能的解決方案,但我沒有發現任何問題。如果使用搜索文本未找到記錄,則會出現錯誤

請幫忙。 謝謝你,更多的權力。

這是代碼。
的index.php

//Perform Search from our database 
    if(isset($_POST['action_type']))     
    {   
     echo 'Welcome'; 
     if ($_POST['action_type'] == 'search') 
     { 
      $search = mysqli_real_escape_string($link, strip_tags($_POST['searchText']));  
      $sql = "select samp_id, daterecv, datecoll, modcoll, aperson, estabname, estabadd, gname, bname, prodcat, dform, dstrength, from sample 
      where gname like '%$search%' or bname like '%$search%' or estabname like '%$search%'"; 
      $result = mysqli_query($link, $sql);  
       if(!$result) 
       { 
       echo mysqli_error($link); 
       exit(); 
      } 

     //Loop through each row on array and store the data to $sample_list[] 
     while($rows = mysqli_fetch_array($result)) 
     { 
      $sample_list[] = array('samp_id' => $rows['samp_id'], 
             'daterecv' => $rows['daterecv'], 
             'datecoll' => $rows['datecoll'], 
             'modcoll' => $rows['modcoll'], 
             'apers' => $rows['apers'], 
             'estabname' => $rows['estabname'], 
             'estabadd' => $rows['estabadd'], 
             'gname' => $rows['gname'], 
             'bname' => $rows['bname'], 
             'prodcat' => $rows['prodcat'], 
             'dform' => $rows['dform'], 
             'dstrength' => $rows['dstrength']); 
     } 
      include 'samplelist.php'; 
      exit(); 
     } 
    } 

samplelist.php

<?php 
session_start(); 
    $role = $_SESSION['sess_userrole']; 
    if(!isset($_SESSION['sess_username']) && $role!="sampler"){ 
     header('Location: index.php?err=2'); 
    } 
?> 
<?php 
include_once 'index.php'; 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <link href="style.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript"> 

function GotoHome(){ 
    window.location = 'index.php'; 
} 
{ 
function preventBack(){window.history.forward();} 
    setTimeout("preventBack()", 0); 
    window.onunload=function(){null}; 
} 
</script> 
</head> 
<body> 

    <div class="wrapper"> 
     <div class="content"> 
      <p><a href="../logout.php" class="link-btn">Logout</a></p> 
      <p><center><b><font style="arial" color="black">SAMPLE COLLECTION</font></b></center></p> 
      <div style="margin-bottom: 10px;"> 
      <form method="POST" action="index.php"> 
       <input type="text" id="searchText" name="searchText" style="width:300px"/> 
       <input type="hidden" name="action_type" value="search" onfocus="Clear (this);"/> 
       <input type="submit" value="search"/> 
      </form> 
      </div> 
      <div style="max-height: 740px; overflow:auto; max-width: 1600; overflow:auto;"> 
      <table class="pbtable"> 
       <thead> 
        <tr> 
         <th> 
          Date Received 
         </th> 
         <th> 
          Date Collected 
         </th> 
         <th style = "display:none"> 
          Mode of Collection 
         </th> 
         <th style = "display:none"> 
          Assigned Personnel 
         </th> 
         <th> 
          Establishment Name 
         </th> 
         <th style = "display:none"> 
          Establishment Address 
         </th> 
         <th> 
          Generic Name 
         </th> 
         <th> 
          Brand Name 
         </th> 
         <th> 
          Product Category 
         </th> 
         <th style = "display:none"> 
          Dosage Form 
         </th> 
         <th style = "display:none"> 
          Dosage Strength 
         </th> 
</thead> 
       <tbody> 
        <?php foreach($sample_list as $collection) : ?> 
         <tr> 
          <td> 
           <?php echo $collection["daterecv"]; ?> 
          </td> 
          <td> 
           <?php echo $collection["datecoll"]; ?> 
          </td> 
          <td style = "display:none"> 
           <?php echo $collection["modcoll"]; ?> 
          </td> 
          <td style = "display:none"> 
           <?php echo $collection["aperson"]; ?> 
          </td> 
          <td> 
           <?php echo $collection["estabname"]; ?> 
          </td> 
          <td style = "display:none"> 
           <?php echo $collection["estabadd"]; ?> 
          </td> 
          <td> 
           <?php echo $collection["gname"]; ?> 
          </td> 
          <td> 
           <?php echo $collection["bname"]; ?> 
          </td> 
          <td> 
           <?php echo $collection["prodcat"]; ?> 
          </td> 
          <td style = "display:none"> 
           <?php echo $collection["dform"]; ?> 
          </td> 
          <td style = "display:none"> 
           <?php echo $collection["dstrength"]; ?> 
          </td> 
     <form method="post" action="index.php"> 
       <input type="hidden" name="ci" 
      value="<?php echo $collection["samp_id"]; ?>" /> 
       <input type="hidden" name="action" value="edit" /> 
       <input type="submit" value="Edit" /> 
     </form> 
      <tr> 
       <?php endforeach; ?> 
    </tbody> 
     </table> 
     </div> 
</div> 

回答

0

如果在DB沒有找到記錄$ sample_list數組爲空。
所以samplelist.php需求foreach循環之前這個檢查,

if (empty($sample_list)) 
{ 
    header('Location: http://www.homesweet.home/'); 
    exit; 
} 

foreach($sample_list as $collection) : 
    ... 
endforeach; 
+0

它的工作好。非常感謝你的幫助。 – Roger

相關問題