SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Complex If Statements + Problems.

    I realize this is a monster of an if() condition, but I can't think of any better way to do it. Basically, I'm checking for all possibilites in the data the user sends which then sorts out data from a database. There are three input types which can be on or off and in any combination. However, when the $_GET['position'] is not set, no matter either of the other variables, the execution of the script halts and I get a generic MySQL error (You have an error near '' in your MySQL syntax at line 1). Any ideas?
    PHP Code:
    if(isset($_GET['submit'])){
        if((isset(
    $_GET['position'])&&$_GET['position']!='')&&(!isset($_GET['state'])||$_GET['state']=='')&&(!isset($_GET['name'])||$_GET['name']=='')){
            
    $SQL='SELECT board_members.*, board_positions.id FROM board_members, board_positions, board_relate '.
                
    'WHERE board_members.id=board_relate.mid AND board_relate.pid=board_positions.id '.
                
    "AND board_positions.id={$_GET['position']}";
            
    LoopDisplay('District Board [Sorted by Position]',$SQL);
        }
        elseif((isset(
    $_GET['position'])&&$_GET['position']!='')&&(isset($_GET['state'])&&$_GET['state']!='')&&(!isset($_GET['name'])||$_GET['name']=='')){
            
    $SQL='SELECT board_members.*, board_positions.id FROM board_members, board_positions, board_relate '.
                
    'WHERE board_members.id=board_relate.mid AND board_relate.pid=board_positions.id '.
                
    "AND board_positions.id={$_GET['position']} AND board_members.city_state_zip LIKE '%{$_GET['state']}%'";
            
    LoopDisplay('District Board [Sorted by Position & State]',$SQL);
        }
        elseif((isset(
    $_GET['position'])&&$_GET['position']!='')&&(isset($_GET['state'])&&$_GET['state']!='')&&(isset($_GET['name'])&&$_GET['name']!='')){
            
    $SQL='SELECT board_members.*, board_positions.id FROM board_members, board_positions, board_relate '.
                
    'WHERE board_members.id=board_relate.mid AND board_relate.pid=board_positions.id '.
                
    "AND board_positions.id={$_GET['position']} AND board_members.city_state_zip LIKE '%{$_GET['state']}%' ".
                
    "AND board_members.name LIKE '%{$_GET['name']}%'";
            
    LoopDisplay('District Board [Sorted by Position, State, & Name]',$SQL);
        }
        elseif((isset(
    $_GET['position'])&&$_GET['position']!='')&&(!isset($_GET['state'])||$_GET['state']=='')&&(isset($_GET['name'])&&$_GET['name']!='')){
            
    $SQL='SELECT board_members.*, board_positions.id FROM board_members, board_positions, board_relate '.
                
    'WHERE board_members.id=board_relate.mid AND board_relate.pid=board_positions.id '.
                
    "AND board_positions.id={$_GET['position']} AND board_members.name LIKE '%{$_GET['name']}%'";
            
    LoopDisplay('District Board [Sorted by Position & Name]',$SQL);
        }
        elseif((!isset(
    $_GET['position'])||$_GET['position']=='')&&(isset($_GET['state'])&&$_GET['state']!='')&&(isset($_GET['name'])&&$_GET['name']!='')){
            
    $SQL='SELECT board_members.name FROM board_members '.
                
    "WHERE board_members.city_state_zip LIKE '%{$_GET['state']}%' AND board_members.name LIKE '%{$_GET['name']}%'";
            
    LoopDisplay('District Board [Sorted by State & Name]',$SQL);
        }
        elseif((!isset(
    $_GET['position'])||$_GET['position']=='')&&(!isset($_GET['state'])||$_GET['state']=='')&&(isset($_GET['name'])&&$_GET['name']!='')){
            
    $SQL='SELECT board_members.*, board_positions.id FROM board_members, board_positions, board_relate '.
                
    'WHERE board_members.id=board_relate.mid AND board_relate.pid=board_positions.id '.
                
    "AND board_members.name LIKE '%{$_GET['name']}%'";
            
    LoopDisplay('District Board [Sorted by Name]',$SQL);
        }
        elseif((!isset(
    $_GET['position'])||$_GET['position']=='')&&(isset($_GET['state'])&&$_GET['state']!='')&&(!isset($_GET['name'])||$_GET['name']=='')){
            
    $SQL="SELECT * FROM board_members WHERE city_state_zip LIKE '%{$_GET['state']}%'";
            
    LoopDisplay('District Board [Sorted by State]',$SQL);
        }

    Thanks.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    put in a line that spits out the sql query after you get the error and post it, it will be easier to tell whats going wrong then. It just might just be an empty variable or something that is screwing it up

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Possible syntax issue

    Uhm...this MAY be a problem, you'll just have to test it.

    I'm not sure if it's valid to concat strings they way you are doing it (switching between single and dbl quotes).
    PHP Code:
    $SQL 'data'.'data'."data"
    However, the bigger problem may be the reason why you are doing it. I see that in the last line of the SQL you reference $_GET['position']. I'm assuming you switched to double quotes so that you may use the singles around the position. Since you are building your SQL statement in a string, it is not necessary to surround the variable hash names with single quotes. Soooo, since that is the variable that is crapping on you, perhaps this will work:
    PHP Code:
    if((isset($_GET['position'])&&$_GET['position']!='')&&(!isset($_GET['state'])||$_GET['state']=='')&&(!isset($_GET['name'])||$_GET['name']==''))
        {
        
    $SQL='SELECT board_members.*, board_positions.id FROM board_members, board_positions, board_relate '.
             
    'WHERE board_members.id=board_relate.mid AND board_relate.pid=board_positions.id '.
             
    'AND board_positions.id=.{$_GET[position]}';
        
    LoopDisplay('District Board [Sorted by Position]',$SQL);
        } 
    I'm not familiar with the syntax you are using around that variable (the {}) however, it looks looks like you have unecessary concatenation. So why not just concatenate where you need to: adding the variable?
    PHP Code:
    if((isset($_GET['position'])&&$_GET['position']!='')&&(!isset($_GET['state'])||$_GET['state']=='')&&(!isset($_GET['name'])||$_GET['name']==''))
        {
        
    $SQL='SELECT board_members.*, board_positions.id FROM board_members, board_positions, board_relate
              WHERE board_members.id=board_relate.mid AND board_relate.pid=board_positions.id
              AND board_positions.id='
    .$_GET['position'];
        
    LoopDisplay('District Board [Sorted by Position]',$SQL);
        } 
    This may not be the solution, but I hope it helps.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    SitePoint Enthusiast menkes's Avatar
    Join Date
    Jul 2002
    Location
    Channel Islands
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Colin,

    Egads what a cluster ****. I could get a migraine looking at that!

    Simplify (at least for the sake of us lazy programmers!).

    Start by doing something like this:

    PHP Code:
    // $pos, $st, $nm are not set anywhere else

    if ((isset($_GET['position']) && ($_GET['position'] != "")){
      
    $pos $_GET['position'];
    }
    if ((isset(
    $_GET['state']) && ($_GET['state'] != "")){
      
    $st $_GET['state'];
    }
    if ((isset(
    $_GET['name']) && ($_GET['name'] != "")){
      
    $nm $_GET['name'];
    }

      if( (
    $pos) && ($nm) && ($st) ){
            
    $SQL="SELECT board_members.*, board_positions.id FROM board_members, board_positions, board_relate ".
                
    "WHERE board_members.id=board_relate.mid AND board_relate.pid=board_positions.id ".
                
    "AND board_positions.id = $pos";
            
    LoopDisplay('District Board [Sorted by Position]',$SQL);
      } 
    Just a thought...I'm sure this could be even more efficient (should serve this for a PHP Tennis round).
    Hope this helps...

  5. #5
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should try using switch() for that many conditionals since that's what it's designed to do.

    also as said above, echo the sql statement right b4 you execute the sql query to see which is being used, and you can try debugging from there.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*


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
  •