我收到此警告> 警告:在<中划分为零如果我输入数据库中不存在的东西。
如果我输入存在的东西一切正常,我无法找到或解决问题。
下面是我目前使用的脚本,我的php skils并不完美,但我尝试了。
<?php
$button = mysql_real_escape_string($_GET ['submit']);
$search = $str = $stt = mysql_real_escape_string(strip_tags($_GET ['search']));
$str = strtoupper($str);
$stt = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
if(strlen($search)<=2) {
echo 'Nothing found for <b>'.strip_tags($str).'</b>. Please try something else ! ';
}else{
echo 'You searched for <b>'.$stt.'</b> ';
echo "";
include 'extern_/connectsearch.php';
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="title LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%'";
}
$constructs ="SELECT * FROM table WHERE $construct";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo 'No results for <b>'.$search.'</b> ';
else
{
$per_page = 36;
$id = (int)mysql_real_escape_string($_GET['page']);
$max_pages = ceil($foundnum / $per_page);
if(!$id)
$id=0;
$p_num = $per_page*$id;
if($id)
$p_num = ($id - 1) * $per_page; //first item to display on this page
else
$p_num = 0;
$getquery = mysql_query("SELECT * FROM `table` WHERE $construct ORDER BY date DESC LIMIT $p_num, $per_page");
$title = $row ['title'];
$url = $row ['url'];
while($row = mysql_fetch_assoc($getquery))
{
echo '<div class="grid">
<a href="'. $row['url'] .'"
id="'. $row['id'] .'"
alt="'. $row['title'] .'"
title="'. $row['title'] .'"
target="_blank">
<h3 class="style">'. substr($row['title'], 0, 34).' ...</h3>
</a>
</div>
</div>
</div>
';
}
}
echo '<center>';
?>
<!-- pagination -->
<?php
echo '<center>';
$page = $id;
$total_pages = $foundnum;
$limit = $per_page;
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$adjacents = 5;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"paginate\">";
if ($page > 1)
$pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$prev\">Prev</a>";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<a href='".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter' class='current'>$counter</a>";
else
$pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<a href='".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter' class='current'>$counter</a>";
else
$pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter\">$counter</a>";
}
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<a href='".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter' class='current'>$counter</a>";
else
$pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter\">$counter</a>";
}
}
else
{
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<a href='".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter' class='current'>$counter</a>";
else
$pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter\">$counter</a>";
}
}
}
if ($page < $counter - 1)
$pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$next\">Next</a>";
$pagination.= "</div>\n";
}
echo $pagination;
echo '</center>';
}
?>
警告:在主页/ search.php 中将其除零 348
第348行
$ lastpage = ceil($ total_pages / $ limit);
我想知道的更多内容是这个脚本是否足够安全?
感谢任何帮助。提前致谢
答案 0 :(得分:1)
您的问题是代码在所有情况下都没有设置 $ per_page 。如果检查代码,您可以看到设置此变量的唯一位置是:
if ($foundnum==0)
但代码仍在运行,执行时
$limit = $per_page;
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
未定义var $ per_limit ,其值为 null 。因此,当您进行除法 $ total_pages / $ limit 时,PHP会将 null 强制转换为 0 ,这就是为什么在没有错误时才会收到此错误的原因返回DB值。
解决方案就像在所有情况下设置 $ per_page 一样简单。