+ Reply to Thread
Results 1 to 4 of 4

Thread: problem with PHP stoping half way

  1. #1
    Barn Newbie jpaytoncfd is an unknown quantity at this point jpaytoncfd's Avatar
    Join Date
    May 2009
    Posts
    27
    Rep Power
    3

    problem with PHP stoping half way

    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
    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; 
    } 
    
    }
    ?>
    
    sorry about the extra comments. using this as an example for a friend who is jut starting. I'm pretty new myself.
    Last edited by mehere; May 23rd, 2009 at 11:20 AM.

  2. #2
    Barn Newbie jpaytoncfd is an unknown quantity at this point jpaytoncfd's Avatar
    Join Date
    May 2009
    Posts
    27
    Rep Power
    3

    also, it dosent work on IE7 any way to fix that?

  3. #3
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Take a look at this sample script: Using PHP to search a MySQL database and return paged results

    Also, the browser you use won't make a difference when it comes to server-side code.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  4. #4
    Barn Newbie jpaytoncfd is an unknown quantity at this point jpaytoncfd's Avatar
    Join Date
    May 2009
    Posts
    27
    Rep Power
    3

    I figured it out.


    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; 
    } 
    
    }
    ?>
    
    that 'die' was making it stop if there where no results.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO