ฉันได้รับคำเตือนนี้ > คำเตือน: หารด้วยศูนย์ใน ‹ หากฉันพิมพ์บางอย่างที่ไม่มีอยู่ในฐานข้อมูล
หากฉันพิมพ์บางอย่างที่มีอยู่ ทุกอย่างทำงานได้ดี ฉันจะค้นหาหรือแก้ไขปัญหาไม่พบ
ด้านล่างนี้คือสคริปต์ที่ฉันใช้ในขณะนี้ ทักษะ php ของฉันไม่สมบูรณ์แบบแต่ฉันก็พยายามแล้ว
<?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="/th'. $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='/th".$_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='/th".$_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='/th".$_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='/th".$_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>';
}
?>
คำเตือน: หารด้วยศูนย์ใน home/search.php ออนไลน์ 348
บรรทัดที่ 348
$lastpage = ceil($total_pages/$จำกัด);
และอีกอย่างที่ฉันอยากรู้คือสคริปต์นี้ปลอดภัยเพียงพอหรือไม่?
ความช่วยเหลือใด ๆ ที่ชื่นชม ขอบคุณล่วงหน้า
5/0
ดังนั้น คุณต้องคำนึงถึงเมื่อมีค่า0
ในส่วนที่เกี่ยวกับif this script safe enough
ดู - stackoverflow.com/questions/60174/ - person Sean   schedule 06.10.2014$limit
เท่ากับ 0 ฉันจะเริ่มต้นด้วยecho $limit;
และดูว่าคุณจะได้อะไร... - person Mark Miller   schedule 06.10.2014$n=1;$d=0; echo $n/$d; // Gives Warning: Division by zero.
- person Sean   schedule 06.10.2014$per_page = 36;
อยู่ภายในelse
ของif ($foundnum==0)
ดังนั้นเมื่อคุณใช้ใน$limit = $per_page;
ดังนั้น$per_page;
จะไม่ได้ถูกกำหนดไว้ และ$lastpage = ceil($total_pages/$limit);
ทำให้$limit
เท่ากับ0
โดยแจ้งเตือนคุณ ลองย้าย$per_page = 36;
ไปด้านนอกif/else
หรือเปลี่ยน$limit = $per_page;
เป็น$limit = 36;
- person Sean   schedule 06.10.2014}
ก่อนecho '<center>
ไปยังจุดสิ้นสุดของไฟล์ - person Sean   schedule 06.10.2014