I have this code and when I search a valad term It works but if what im searching dosent exist I want it to show the error message but if there are no results the php just stops and ignores the rest of the code
sorry about the extra comments. using this as an example for a friend who is jut starting. I'm pretty new myself.Code:<h2>Search</h2> <form method="post" action="<?=$PHP_SELF?>"> Seach for: <input type="text" name="find" /> <input type="hidden" value="id" <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <? //first of having the double / makes this line of text be ignored by the server. //these are called comments and they serve no purpous to the html thats is sent to the users browser //extra empty line breaks do nothing ether. although they are included in the line count as well as comments //this makes $find= what was entered into the search box $find=$_POST["find"]; //this makes $searching the value of the hidden field searching which // is only used to hide this part untill you enter a search $searching=$_POST["searching"]; //This is only displayed if they have submitted the form //this is the purpous of the hidden field searching //the rest of the code for the results is contained in here if ($searching =="yes") { echo "<h2>Results</h2><p>"; //If they did not enter a search term we give them an error. //notice the 'if' if $find = nothing then run whats within the following {} //and exit makes the PHP stop there and not go on. //if the 'if' is not true it moves past the {} if ($find == "") { //echo will display the text in the "" or the result on an array for example $find echo "<p>Please Enter An Error Code"; exit; } // Otherwise we connect to our Database (host, username, password) or die tells the code to show a php error mysql_connect("******", "******", "******") or die(mysql_error()); // once logged onto the DB this selects the specific DB set in my case its the same as my user name mysql_select_db("******") or die(mysql_error()); // We preform a bit of filtering // $find is what you typed into the search bar. // these 3 lines make it all uppercase, strips any code, and removes spaces $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search for our search term supplied in the search box. // this will make the results of the search be accessable as $data // FROM= the table, WHERE= the colum, and the '=' means the row that has the search term in the ID colum $data = mysql_query ("SELECT * FROM dmx WHERE ID LIKE '$find'"); //And we display the results //This turns the $data (which is only a code for the row on the table) into useable text. $info = mysql_fetch_array ( $data ) or die(mysql_error()); { //this will show the word 'code:' followed by what ever was in the search box echo "<b>Code:</b> " .$find. "<br>"; //this uses the results from the database row defined by $info and the colum data //$info= row ['data']= colum. echo "<b>Description:</b> " .$info['data']. "<br>"; } //This counts the number or results - and if there wasn't any it gives them a little message explaining that //still trying to get this to work //this counts the nuber of rows which = $anymatches $anymatches = mysql_num_rows($data); if ($anymatches == "0") { echo "Sorry, no results. (be sure you have the whole error code)<br><br>"; echo "<b>Searched For:</b> " .$find; } } ?>



LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks