johanlundin88 Posted November 29, 2007 Share Posted November 29, 2007 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); } ?> Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 29, 2007 Share Posted November 29, 2007 You simply need to check the search string, and if it is empty, don't run your search: <?php // Change this line: if(isset($_POST['namn'])) // to this: if(isset($_POST['namn']) && !empty($_POST['namn'])) ?> Quote Link to comment Share on other sites More sharing options...
johanlundin88 Posted November 29, 2007 Author Share Posted November 29, 2007 This shange closes the whole script? I still want to run the search, but without the empty searchstring. I dont want to serch in 'namn' if that search string is empty, but I still want to search in 'id' and 'status'. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 29, 2007 Share Posted November 29, 2007 That wasn't the exact code, it was to give you an idea on how to do your code to make it work. Quote Link to comment Share on other sites More sharing options...
johanlundin88 Posted November 29, 2007 Author Share Posted November 29, 2007 ok, but I still don't know how to do it.. Something like this? if (empty($_POST['namn'])) { What should I write here? } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.