Jump to content

Search script don't search empty values


johanlundin88

Recommended Posts

I'm trying to make a search script. I don't want the script to search for empty values if you don't write anything in one of the searchfields.

Does anyone know how to do this, or where I can find examples of advanced searchscripts?

 

This is the script so far:

<form method="post" action="sok.php">
Namn:<br>
<input name="namn" type="text"><br>
ID:<br>
<input name="id" type="text"><br>
Status:<br>
<select name="status">
<option value="">«Ange status»</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

</select>
<br><br>
<input type="submit" value="Sök">
</form>

<?php


// hämtar information från den angivna tabellen
$result = mysql_query("SELECT * FROM personer WHERE id='$_POST[id]'
OR namn LIKE '$_POST[namn]' OR status='$_POST[status]'")

or die(mysql_error());


if(isset($_POST['namn'])) {

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($result);
if ($anymatches == 0)
{
echo "<br>Tyvärr, kunde vi inte hitta något. Testa söka på något annat.<br><br>
";
}
else
{
echo "<br><b>Reslutat: $anymatches</b>
";
}

// HTML-tabellens formatering - tabellstart

echo "<br><table border='1' cellspacing='0' cellpadding='3'>";


// hämtar resultatrader från tabellen
while($row = mysql_fetch_array( $result ))
{
// skriver ut innehållet i raderna till HTML-tabellen
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo "<span class=\"namn\"><a href='http://www" . $row['id'] . "'>".$row["namn"]."</a></span>";
echo "</td><td>";
echo $row['status'];
echo "</td></tr>";
}



// HTML-tabellens formatering - tabellslut
echo "</table>";




// stänger databasen
mysql_close($opendb);

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/79423-search-script-dont-search-empty-values/
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.