第二个搜索输入字段的未定义变量

时间:2017-03-22 00:35:50

标签: php mysqli

我希望使用我的表单来搜索数据库中的第二列,下面的当前代码将搜索名为companyname的列,但第二列不会执行任何操作,我会收到错误**注意:未定义的变量:strKeyword2 in第52行(选择行)你能否提供一些帮助,因为我明显做错了什么。非常感谢 有我的表格和代码

  Form
  <input name="companyname" type="text" id="txtKeyword" value="">
  <input name="location" type="text"  id="txtKeyword2" size="20" value=""/>
  <input type="submit" value="Search">
  form end

$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);

ini_set('display_errors', 1);
    error_reporting(~0);

    $strKeyword = null;

    if(isset($_POST["companyname"])){
        $strKeyword = $_POST["companyname"];
    }
    if(isset($_POST["location"])){
        $strKeyword2 = $_POST["location"];
    }
    if(isset($_GET["companyname"])){
        $strKeyword = $_GET["companyname"];         
    }
    if(isset($_GET["location"])){
        $strKeyword2 = $_GET["location"];           
    }

    $sql = "SELECT * FROM reviewsandtips WHERE (companyname LIKE '%".$strKeyword."%' AND location LIKE '%".$strKeyword2."%' ) ";


$query = mysqli_query($conn,$sql);

$num_rows = mysqli_num_rows($query);

$per_page = 2;   
$page  = 1;


if(isset($_GET["Page"]))
{
    $page = $_GET["Page"];
}

$prev_page = $page-1;
$next_page = $page+1;

$row_start = (($per_page*$page)-$per_page);
if($num_rows<=$per_page)
{
    $num_pages =1;
}
else if(($num_rows % $per_page)==0)
{
    $num_pages =($num_rows/$per_page) ;
}
else
{
    $num_pages =($num_rows/$per_page)+1;
    $num_pages = (int)$num_pages;
}
$row_end = $per_page * $page;
if($row_end > $num_rows)
{
    $row_end = $num_rows;
}

$sql .= " ORDER BY companyname ASC LIMIT $row_start ,$row_end ";
$query = mysqli_query($conn,$sql);

?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">location </div></th>
<th width="97"> <div align="center">review </div></th>
</tr>
<?php
 while($result=mysqli_fetch_array($query,MYSQLI_ASSOC))
 {
  ?>
 <tr>
<td><div align="center"><?php echo $result["member_id"];?></div></td>
<td><?php echo $result["companyname"];?></td>
<td><?php echo $result["location"];?></td>
<td><div align="center"><?php echo $result["reviewsubmission"];?></div></td>
 </tr>
 <?php
 }
  ?>
 </table>
  <br>
   Total <?php echo $num_rows;?> Record : <?php echo $num_pages;?> Page :
  <?php
  if($prev_page)
  {

     echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$prev_page&txtKeyword=$strKeyword'><< Back</a> ";
   }

 for($i=1; $i<=$num_pages; $i++){
  if($i != $page)
   {
    echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$strKeyword'>$i</a> ]";
 }
  else
  {
     echo "<b> $i </b>";
  }
  if($page!=$num_pages)
  {
    echo " <a href ='$_SERVER[SCRIPT_NAME]?P age=$next_page&txtKeyword=$strKeyword'>Next>></a> ";
  }
 $conn = null;

0 个答案:

没有答案