我的代码存在小问题。我有一个搜索框,它使用多个变量作为数据库进行搜索,一切正常,显示正常。我的问题在于文本框中没有条目,然后我得到以下错误:
strpos()[function.strpos]:空分隔符
在设置变量之前,有没有办法检查文本框是否为空?
这是我的代码:
if(isset($_POST["searchSub"])) {
$searchTerm = $_POST["searchTb"];
$pos1 = strpos($tBody1, $searchTerm);
$pos2 = strpos($tBody2, $searchTerm);
$pos3 = strpos($tBody3, $searchTerm);
$pos4 = strpos($tBody4, $searchTerm);
$pos5 = strpos($tBody5, $searchTerm);
$pos6 = strpos($tBody6, $searchTerm);
$pos7 = strpos($tBody7, $searchTerm);
$pos8 = strpos($tBody8, $searchTerm);
$pos9 = strpos($tBody9, $searchTerm);
$pos10 = strpos($tBody10, $searchTerm);
$pos11 = strpos($tBody11, $searchTerm);
$pos12 = strpos($tBody12, $searchTerm);
$pos13 = strpos($tBody13, $searchTerm);
$pos14 = strpos($tBody14, $searchTerm);
$pos15 = strpos($tBody15, $searchTerm);
$pos16 = strpos($tBody16, $searchTerm);
$pos17 = strpos($tBody17, $searchTerm);
$pos18 = strpos($tBody18, $searchTerm);
$pos19 = strpos($tBody19, $searchTerm);
$pos20 = strpos($tBody20, $searchTerm);
}
答案 0 :(得分:0)
只需使用empty()而不是isset()
答案 1 :(得分:0)
在尝试使用之前,您需要确保已输入相关字段
if(isset($_POST["searchSub"]) && !empty($_POST["searchTb"])) {
searchTerm = $_POST["searchTb"];
$pos1 = strpos($tBody1, $searchTerm);
$pos2 = strpos($tBody2, $searchTerm);
$pos3 = strpos($tBody3, $searchTerm);
$pos4 = strpos($tBody4, $searchTerm);
$pos5 = strpos($tBody5, $searchTerm);
$pos6 = strpos($tBody6, $searchTerm);
$pos7 = strpos($tBody7, $searchTerm);
$pos8 = strpos($tBody8, $searchTerm);
$pos9 = strpos($tBody9, $searchTerm);
$pos10 = strpos($tBody10, $searchTerm);
$pos11 = strpos($tBody11, $searchTerm);
$pos12 = strpos($tBody12, $searchTerm);
$pos13 = strpos($tBody13, $searchTerm);
$pos14 = strpos($tBody14, $searchTerm);
$pos15 = strpos($tBody15, $searchTerm);
$pos16 = strpos($tBody16, $searchTerm);
$pos17 = strpos($tBody17, $searchTerm);
$pos18 = strpos($tBody18, $searchTerm);
$pos19 = strpos($tBody19, $searchTerm);
$pos20 = strpos($tBody20, $searchTerm);
}
答案 2 :(得分:0)
你应该添加像turnery这样的条件。
if(isset($_POST["searchSub"]) && !empty($_POST["searchTb"])) {
$searchTerm = $_POST["searchTb"];
$pos1 = (tBody1 != "")?strpos($tBody1, $searchTerm):"NULL";
$pos2 = (tBody2 != "")?strpos($tBody2, $searchTerm):"NULL";
$pos3 = (tBody3 != "")?strpos($tBody3, $searchTerm):"NULL";
$pos4 = (tBody4 != "")?strpos($tBody4, $searchTerm):"NULL";
$pos5 = (tBody5 != "")?strpos($tBody5, $searchTerm):"NULL";
$pos6 = (tBody6 != "")?strpos($tBody6, $searchTerm):"NULL";
$pos7 = (tBody7 != "")?strpos($tBody7, $searchTerm):"NULL";
$pos8 = (tBody8 != "")?strpos($tBody8, $searchTerm):"NULL";
$pos9 = (tBody9 != "")?strpos($tBody9, $searchTerm):"NULL";
$pos10 = (tBody10 != "")?strpos($tBody10, $searchTerm):"NULL";
$pos11 = (tBody11 != "")?strpos($tBody11, $searchTerm):"NULL";
$pos12 = (tBody12 != "")?strpos($tBody12, $searchTerm):"NULL";
$pos13 = (tBody13 != "")?strpos($tBody13, $searchTerm):"NULL";
$pos14 = (tBody14 != "")?strpos($tBody14, $searchTerm):"NULL";
$pos15 = (tBody15 != "")?strpos($tBody15, $searchTerm):"NULL";
$pos16 = (tBody16 != "")?strpos($tBody16, $searchTerm):"NULL";
$pos17 = (tBody17 != "")?strpos($tBody17, $searchTerm):"NULL";
$pos18 = (tBody18 != "")?strpos($tBody18, $searchTerm):"NULL";
$pos19 = (tBody19 != "")?strpos($tBody19, $searchTerm):"NULL";
$pos20 = (tBody20 != "")?strpos($tBody20, $searchTerm):"NULL";
}
希望这将是您的解决方案。 :)