Php 如果使用searchtext未找到记录,则搜索时会出现错误
如果找到记录,SearchText会工作,但如果找不到记录,则会出现错误。 以下是错误。注意:未定义变量:C:\xampp\htdocs\viewsample\samplelist.php中的sample\u list,第166行 警告:为C:\xampp\htdocs\viewsample\samplelist.php中的foreach()提供的参数无效,第166行 如果我再次单击搜索按钮,错误将消失。 如果找不到记录,是否可以返回主页? 我在这里寻找解决我问题的可能办法,但没有找到 请帮忙。 谢谢你,还有更多的力量 这是代码。Php 如果使用searchtext未找到记录,则搜索时会出现错误,php,search,Php,Search,如果找到记录,SearchText会工作,但如果找不到记录,则会出现错误。 以下是错误。注意:未定义变量:C:\xampp\htdocs\viewsample\samplelist.php中的sample\u list,第166行 警告:为C:\xampp\htdocs\viewsample\samplelist.php中的foreach()提供的参数无效,第166行 如果我再次单击搜索按钮,错误将消失。 如果找不到记录,是否可以返回主页? 我在这里寻找解决我问题的可能办法,但没有找到 请帮忙。
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>
函数GotoHome(){
window.location='index.php';
}
{
函数preventBack(){window.history.forward();}
setTimeout(“preventBack()”,0);
onunload=function(){null};
}
样本采集
收到日期
收集的日期
收集方式
指派人员
机构名称
机构地址
通用名
品牌名称
产品类别
剂型
剂量强度
如果在DB中未找到任何记录,$sample_list数组为空。
所以samplelist.php需要在foreach循环之前进行检查
if (empty($sample_list))
{
header('Location: http://www.homesweet.home/');
exit;
}
foreach($sample_list as $collection) :
...
endforeach;