0

I am looking to use my form to search a second column in the database, the current code below will search the column called companyname but the second does not do anything and i get an error **Notice: Undefined variable: strKeyword2 in line 52 (SELECT LINE) Could you please provide some help as I'm clearly doing something wrong. Many thanks There is my form and code

  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;
phpnewbe
  • 11
  • 2
  • The $strKeyword variable is clearly not set. So you are not receiving location with neither POST or GET. Do you literally just write "Form" and "form end" or use something like `
    ` and `
    `?
    – Yolo Mar 22 '17 at 00:59
  • hi Yolo my form does have
    – phpnewbe Mar 22 '17 at 19:34
  • Unfortunetly this post is marked as duplicate. There is a couple of things going on in your code: 1. remove the `error_reporting(~0);` line. You should see what errors are thrown. 2. Inititialize the $strKeyword to be an empty string like so: `$strKeyword = ''; $strKeyword2 = ''; $num_rows = 0;` 3. place both post and get conditions in another wrapper: `if ($_SERVER['REQUEST_METHOD'] == 'GET') { if(isset($_GET["companyname"])){ // .... } }` and `if ($_SERVER['REQUEST_METHOD'] == 'POST') { if(isset($_POST["companyname"])){ // .... } }` – Yolo Mar 22 '17 at 21:57
  • 4. VeryEnd is missing `}` – Yolo Mar 22 '17 at 22:07

0 Answers0