Results 1 to 12 of 12

Thread: How to check for empty string and empty search type?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    How to check for empty string and empty search type?

    Hi all . I wonder how make sure the user selected search type m and typed search term s. If they do not i post them this massage along with search box again and rest of scrip does not run:

    ::: Invalid Search :::

    You can not search for empty string, your search word must be at least 3 character long. Try again please



    Code:
    <table border="0" cellspacing="0" width="90%" id="table1"
    style="border-style:dashed&gt;
    	&lt;tr&gt;
    		&lt;td&gt;
    		&lt;p align="
    center">
        <tr>
            <td><b><i>::: Search </i></b><font color="#FF0000"><b><i>New</i></b></font>
            ::: <br>
            <form action="searchprocess.php" method="GET" name="form1">
                <p><input type="text" size="20" name="s">  </p>
    
    <select name="m">
    		    <option selected>Select a search type</option>
    <option value="artist">Search By Artist</option>
    <option value="title">Search By Title</option>
    <option value="album">Search by Album</option>
    
              </select>
    <input
                type="submit" > </p>
            </form>
            </td>
        </tr>
    </table>
    PHP PART
    PHP Code:

    <?
    error_reporting(E_ALL); 

    $m   = $HTTP_GET_VARS['m'];

    $s   = $HTTP_GET_VARS['s'];



    switch ($m){
     case "title": 
    $query  = "select id,artist,album,songname from musictest where songname like '%$s%'";
      break;
     case "album": 

    $query  = "select DISTINCT album, artist from musictest where album like '%$s%' ";

      break;
     case "artist": 
     $query  = "select DISTINCT artist from musictest where artist like '%$s%' ";
      break;
    }

    $result = mysql_query($query);
    if (! ($result = mysql_query($query))) {
        echo(mysql_error());
        exit;
    }
    ?>
    standred html for page view and using switch to setup diffrent table for 
    each type of search
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="./default.css">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title> </title>
    </head>
    <body topmargin="0" leftmargin="0" ONLOAD="preloadImages();">

    .....
    .....
    <? switch ($m):case "title": ?>
     <tr><tr>
            <td width="5%"><b>#</b></td>
            <td width="30%"><b>Song</b></td>
            <td width="20%"><b>In Album</b></td>
            <td width="20%"><b>By Singer</b></td>
            
        </tr>

    .....
    .....

    <? endswitch; ?>

    <?
    $numbers=0;
    while($row = mysql_fetch_assoc($result))
    {

     // again usage of switch for each type of serach type output to make the table 
    ....
    ....
    <? endswitch; ?>

    <?


    } // end of while loop


    ?>


    <?
    switch ($m):
     case "title": ?>

      </table>


     <? break;
     case "artist": ?>
        
    </table>
     <? break;
     case "album": ?>
         </table>
    <? break;
     default: ?>
     this is some default html

    <? endswitch; ?>

    </body>

    </html>
    Last edited by tony007; May 24th, 2006 at 12:18 AM.

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: How to check for empty string and empty search type?

    You could just use the strlen and trim functions and if it returns 0, then display an error.

    References:

    http://ca.php.net/manual/en/function.strlen.php

    http://ca.php.net/manual/en/function.trim.php

    HTH

  3. #3
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: How to check for empty string and empty search type?

    You should use isset() in the first place to make sure that the variable is set in the first place, this will prevent warning messages being printed out by PHP.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to check for empty string and empty search type?

    Quote Originally Posted by john tindell
    You should use isset() in the first place to make sure that the variable is set in the first place, this will prevent warning messages being printed out by PHP.
    well as u see from my code i got lots of html and php mixed code . could u show me how to use it ?Thanks

  5. #5
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: How to check for empty string and empty search type?

    Just before you go to run your query, use

    PHP Code:
    If (isset($m) && isset($s))
    {

    code



    isset simply checks to see if there is a value within the variable. If it contains null, it returns false.


    HTH

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to check for empty string and empty search type?

    kfcSmitty thank u for u reply. do u mean put the whole switch inside if loop?.I already get error if m and s are empty but not customed error and no black form!!! and i wonder what does this code do !!!!

    What i am realy looking for a solution that allow me print a customed massage and pring blank search form.if either m or s or both are nulls how i can display a cusstomed errror with a seach form again and avoid running the rest of my code ? what i mean the rest of the code is the mixed html and php code after $result = mysql_query($query); that i did not post in my first post. It is huge code and i wonder how to deal with them since it is dam confusing to put them inside if loop if it is required!! so i be happy if u show me a some simple way to avoid putting large mixed code inside if loop.Thanks
    Last edited by tony007; May 23rd, 2006 at 02:25 PM.

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: How to check for empty string and empty search type?

    The HTTP_*_VARS globals have been deprecated, so you neet to use $_GET:
    PHP Code:

    $m   
    $_GET['m'];

    if (isset(
    $_GET['s'])) {
        
    $s   $_GET['s'];
        
        if(
    strlen($s) < 3) {
            
    $error "
    You can not search for empty string, your search word must be at least 3 character long. Try again please "
        


    You must also use mysql_escape_string, before embedding the search term in a wquery, otherwise characters in the search term may be interpreted as SQL. This can crash your app or worse if the person at the other end constructs a search term with addittional SQL, allow them to view information about your database and the system it runs one.
    Last edited by visualAd; May 24th, 2006 at 02:02 AM.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to check for empty string and empty search type?

    visual ad how to check for m value which is search type ? how to use mysql_escape_string?

    It keeps giving me error:
    Parse error: syntax error, unexpected in bold parts!

    if ( isset($_GET['s']) )
    {
    $s = $_GET['s'];

    if(strlen($s) < 3) )
    {
    $error = " You can not search for empty string, your search word must be at least 3 character long. Try again please "
    }
    }
    Last edited by tony007; May 23rd, 2006 at 06:20 PM.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How to check for empty string and empty search type?

    I have re written your code for you. Take note of the layout and techniques I've used. Your original code was a bit messy and you executed the query twice for some reason.

    PHP Code:
    <?php
      error_reporting
    (E_ALL);

      
    /*
        -- explaining the following if() condition: --
        if get 'm' and get 's' are both set, then set $mode to $_GET['m']
          and $searchTerm to $_GET['s'],
        proceed if trimmed length of both is greater than zero
      */
      
      
    if (
        (isset(
    $_GET['m']) && isset($_GET['s'])) &&
        
    strlen(trim($mode $_GET['m'])) > && strlen(trim($searchTerm $_GET['s'])) > 0
      
    ) {  
        switch (
    $mode) {
          case 
    'title':
            
    $query =  
              
    'select `id`, `artist`, `album`, `songname` '.
              
    'from `musictest` '.
              
    'where `songname` like \''.$searchTerm.'\'';
            break;
          case 
    'album':
            
    $query =
              
    'select distinct `album`, `artist` '.
              
    'from `musictest` '.
              
    'where `album` like \''.$searchTerm.'\'';
            break;
          case 
    'artist':
            
    $query 
              
    'select distinct `artist` '.
              
    'from `musictest` '.
              
    'where `artist` like \''.$searchTerm.'\'';
            break;
          default:
            
    // unrecognised search mode
        
    }
        
        
    $result mysql_query($query);
        
        if (
    is_resource($result)) {
          if (
    mysql_num_rows($result) > 0) {
            while (
    $row mysql_fetch_assoc($result)) {
              
    // output results
            
    }
          }
          else {
            
    // no results found
          
    }
        }
        else {
          
    // query error
          
    echo mysql_error();
        }
      }
      else {
        
    // mode or search term not specified
      
    }
    ?>
    You will have to fill in the code, to output the appropriate messages for each error condition.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to check for empty string and empty search type?

    penagate i do not think the above code will help me since i got over 200 php and html mixed lines of code and displaying diffrent tables for each type of search output and just puting that inside while loop will not make my program to work!! I get tones of errrors :-((.

    I was looking for solution just like break used in switch whenever error occoured or either s or m was empty but apperently i do not get such solution!!!

    I got html and php mixed code before while loop and inside while loop and after while loop to display proper table for each type of search type output .Also no connection infor in u code such as this :


    PHP Code:
    // change below is your assigned mySQL username
    $user "root";

    // change to the pw below is your assigned mySQL password
    $pw "";

    // change to the database you have permission to connect to
    $db "test";



    //mysql_connect("localhost", "root", ""); 
    $mysql_access mysql_connect("localhost"$user$pw);
    mysql_select_db($db$mysql_access);

    if (! 
    mysql_select_db($db$mysql_access)) {
        echo(
    mysql_error());
        die;
    }

    .....
    .....
    ..... 
    Last edited by tony007; May 24th, 2006 at 12:08 AM.

  11. #11
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: How to check for empty string and empty search type?

    I suggest you go away and learn PHP.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  12. #12
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: How to check for empty string and empty search type?

    Also, it is never a good idea to run your php scripts through root! I suggest you create a new user with just enough permissions to run your script. Otherwise, if someone hacks your script, they can get complete control of your system.
    Last edited by kfcSmitty; May 24th, 2006 at 07:56 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width