Jump to content

Searching mysql


cordoprod

Recommended Posts

Hi.

I am struggling with my search page.

 

I don't think the query is right.

Take a look at line 60-100.

 

finn.php

<?PHP
head("stage/Find-Search", "Søk i medlemmer");
?>
<form action="index.php?s=finn" method="post" name="form_finn" id="form_finn">
<table width="98%" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <td>Brukernavn</td>
    <td><label>
      <input type="text" name="finn_brukernavn" id="finn_brukernavn">
      <input name="finn_nbruker" type="checkbox" id="finn_nbruker" value="true">
      Nøyaktig brukernavn?
    </label></td>
  </tr>
  <tr>
    <td>Alder</td>
    <td><label>
      <select name="finn_alder_fra" id="finn_alder_fra">
      <?PHP
  for($fra=0;$fra<date("Y")-1920;$fra++) {
  echo "<option value=\"$fra\">$fra år</option>";
  }
  ?>
      </select>
    til 
    <select name="finn_alder_til" id="finn_alder_til">
    <?PHP
  for($til=0;$til<date("Y")-1920;$til++) {
  echo "<option value=\"$til\">$til år</option>";
  }
?>
    </select>
    </label></td>
  </tr>
  <tr>
    <td>Kjønn</td>
    <td><label>
      <input name="finn_gutt" type="checkbox" id="finn_gutt" value="true" checked>
    Gutt </label><label>
    <input name="finn_jente" type="checkbox" id="finn_jente" value="true" checked>
    Jente</label></td>
  </tr>
  <tr>
    <td>Postnummer</td>
    <td><label>
      <input name="finn_postnummer" type="text" id="finn_postnummer" size="4" onChange="hent_bosted(this.value)">
    </label> <span id="postbox"></span></td>
  </tr>
  
  <tr>
    <td> </td>
    <td><label>
      <input type="submit" name="finn_submit" id="finn_submit" value="Søk">
    </label></td>
  </tr>
</table>
<br>
</form>
<?PHP

if(isset($_POST['finn_submit'])) {
  // Get the search variable from URL

  $brukernavn = trim($_POST['finn_brukernavn']);
  $nbrukernavn = $_POST['finn_nbruker'];
  $fra = $_POST['finn_fra'];
  $til = $_POST['finn_til'];
  $gutt = $_POST['finn_gutt'];
  $jente = $_POST['finn_jente'];
  $postnummer = $_POST['finn_postnummer'];
  $palogget = $_POST['finn_palogget'];
  $opptatt = $_POST['finn_opptatt'];
  $borte = $_POST['finn_borte'];
  $avlogget = $_POST['finn_avlogget'];
  
  if($nbrukernavn) $q_brukernavn = "brukernavn='$brukernavn'";
  else $q_brukernavn = "brukernavn LIKE \"$brukernavn\"";
  
  $q_fra = "";
  $q_til = "";
  
  if($jente == true and $gutt == true) $q_jente = " AND kjonn='Gutt' OR kjonn='Jente'";
  elseif($gutt) $q_gutt = " AND kjonn='Gutt'";
  elseif($jente) $q_jente = " AND kjonn='Jente'";
  
  
  if($postnummer != "") $q_postnummer = " AND postnummer=''";

// rows to return
$limit=50; 

//connect to your database ** EDIT REQUIRED HERE **
$db = new DbConnector();
$db->connect();

// Build SQL Query  
$query = "SELECT * FROM users WHERE $q_brukernavn$q_gutt$q_jente$q_postnummer ORDER BY id DESC"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=$db->query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{
    smallerror("Ingen resultater.");
}

// 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 = $db->query($query);

// begin to show results set
if($numrows > 0) smallinfo("Fant $numrows profiler.");
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
   
   echo "<br>
<table align=\"center\" width=\"98%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\" class=\"tbl\">
    <tr>
      <td width=\"5%\" rowspan=\"3\" valign=\"top\"><div class=\"profilbilde\">";
  if(file_exists("img/profil/{$row['brukernavn']}.jpg")) { echo "<img src=\"img/profil/{$row['brukernavn']}_tiny.jpg\" width=\"60\" height=\"74\">"; } 
  else { echo "<img src=\"img/profil/no_picture_tiny.jpg\" width=\"60\" height=\"74\">"; } echo "</div></td>
      <td width=\"95%\"><h2 style=\"font-size: 18px\">".ucfirst($row['brukernavn'])." ".alder($row['bursdag'])." ".kjonn($row['brukernavn'])."</h2></td>
    </tr>
    <tr>
      <td valign=\"top\"><strong>Sist innlogget:</strong> ".sist($row['login'])."<br>
  <strong>Bosted:</strong> ".sted($row['postnummer'])."<br>
<br>
<br><br>

</td>
    </tr>
  </table>";
  
  $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 " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&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 " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>"; 
}
?>

 

I don't get any error messages, but it only search if there is something entered in finn_brukernavn. Even though there are results

Link to comment
https://forums.phpfreaks.com/topic/144234-searching-mysql/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.