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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.