Hello
I am trying to create a one-field search form with fields that match fields in a database, carry out a search for records which match the word put in the search button, and show the results in a table on the same page.
I have got myself a bit mixed up. Something does show, but it just repeats for all the records I have. I actually want it to list the fields in the database, but dont'know how to write the sql query property.
My table is called address_details
Has the following fields: address_owner, block_no, unit_no, road_name, house_name, local_area, postcode, price, property_status.

Any help would be much appreciated.

<?php require_once('./Connections/iwalletc_localhost.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$maxRows_rs_search = 10;
$pageNum_rs_search = 0;
if (isset($_GET['pageNum_rs_search'])) {
  $pageNum_rs_search = $_GET['pageNum_rs_search'];
}
$startRow_rs_search = $pageNum_rs_search * $maxRows_rs_search;

mysql_select_db($database_iwalletc_localhost, $iwalletc_localhost);
$query_rs_search = "SELECT * FROM address_details";
$query_limit_rs_search = sprintf("%s LIMIT %d, %d", $query_rs_search, $startRow_rs_search, $maxRows_rs_search);
$rs_search = mysql_query($query_limit_rs_search, $iwalletc_localhost) or die(mysql_error());
$row_rs_search = mysql_fetch_assoc($rs_search);

if (isset($_GET['totalRows_rs_search'])) {
  $totalRows_rs_search = $_GET['totalRows_rs_search'];
} else {
  $all_rs_search = mysql_query($query_rs_search);
  $totalRows_rs_search = mysql_num_rows($all_rs_search);
}
$totalPages_rs_search = ceil($totalRows_rs_search/$maxRows_rs_search)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body><form action='<?php echo $_SERVER['PHP_Self']; ?>'>
  <input type="text" name="q" />
  <input type="submit" name="Submit" value="Search" />
</form>

<?php do { ?>
  <table width="455" border="1" cellspacing="0" cellpadding="0">
    <tr>
      <td height="33"><?php

  // Get the search variable from URL
  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","iwalletc","xxxxxxxx"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("iwalletc_property") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "select * from address_details where property_status like \"%$trimmed%\"
  order by property_status"; // EDIT HERE and specify your table and field names for the SQL query

 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);


// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: &quot;" . $var . "&quot;</p>";

// begin to show results set
echo "Results";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["property_status" + "price"];

  echo "$count.)&nbsp;$title" ;
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
  Prev 10</a>&nbsp&nbsp;";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?></td>
    </tr>
  </table>
  <?php } while ($row_rs_search = mysql_fetch_assoc($rs_search)); ?>
</body>
</html>
<?php
mysql_free_result($rs_search);
?>

Hi,

You can debug your query (echo $query after line 113) and check in your database if you are retrieving the correct results.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.